[英]Close a txt file opened by another process in Windows programmatically
我希望強制關閉由另一個進程打開的 txt 文件。 尋找有關如何實現這一點的建議、想法以及所涉及的注意事項。
Python 解決方案會很棒,但是其他編程語言也很好。 我能夠列出該進程以及該進程打開了多少文件,但對於如何關閉該文件卻是空白。
如果您想在不殺死其他進程的情況下執行此操作,則需要使用NtQueryInformationProcess
來獲取該進程打開的HANDLE
列表。 您還可以使用NtQuerySystemInformation
來獲取打開的HANDLE
的全局列表。 然后只需使用NtDuplicateObject.
關閉HANDLE
。
參見: https://github.com/Zer0Mem0ry/WindowsNT-Handle-Scanner/blob/master/FindHandles/main.cpp
你可以試試這個,使用 Python 按名稱殺死進程:
import sys, traceback, os
def py_kill (process_name):
try:
killed = os.system('tskill ' + process_name)
except Exception, e:
killed = 0
return killed
你可以這樣稱呼它:
py_kill("program_name")
使用 C# 通過 PID 殺死進程:
using System;
using System.Diagnostics;
using System.Text.RegularExpressions;
namespace killprocess
{
class Program
{
static void Main(string[] args)
{
if(args.Length!=1)
{
Console.WriteLine(“Argument is required: Process ID to kill”);
return;
}
Regex regex = new Regex(“^[0-9]+$”);
bool bb = regex.IsMatch(args[0]);
if (!bb)
{
Console.WriteLine(“Process Id should be prositive integer”);
return;
}
int pid = Convert.ToInt32(args[0]);
try
{
nbsp; Process process2Kill = Process.GetProcessById(pid);
process2Kill.Kill();
Console.WriteLine(String.Format(“Process with ID {0} has been terminated”, pid));
} catch (Exception ex)
{
Console.WriteLine(ex.Message);
;}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.