我想强制关闭由另一个进程打开的 txt 文件。寻找关于如何实现这一点的建议、想法以及相关的注意事项。
Python 解决方案会很棒,但其他编程语言也很好。我能够列出该进程,以及该进程打开了多少文件,但对于如何关闭该文件一无所知。
最佳答案
您可以试试这个,使用 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);
;}
}
}
}
关于java - 以编程方式关闭 Windows 中另一个进程打开的 txt 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58648923/