我想通过vb.net使用完整文件路径终止应用程序,但我在Process下找不到它。我希望有一个简单的Process.Stop(文件路径),就像Process.Start一样,但没有这样的运气。
我该怎么办?
答案 0 :(得分:2)
试
System.Diagnostics.Process.GetProcessesByName(nameOfExeFile)。首先()。杀()
这会忽略文件的路径。
答案 1 :(得分:2)
你必须查看每个进程的Modules属性,然后根据你想要的路径检查文件名。
以下是一个例子:
<强> VB.NET 强>
Dim path As String = "C:\Program Files\Ultrapico\Expresso\Expresso.exe"
Dim matchingProcesses = New List(Of Process)
For Each process As Process In process.GetProcesses()
For Each m As ProcessModule In process.Modules
If String.Compare(m.FileName, path, StringComparison.InvariantCultureIgnoreCase) = 0 Then
matchingProcesses.Add(process)
Exit For
End If
Next
Next
For Each p As Process In matchingProcesses
p.Kill()
Next
<强> C#强>
string path = @"C:\Program Files\Ultrapico\Expresso\Expresso.exe";
var matchingProcesses = new List<Process>();
foreach (Process process in Process.GetProcesses())
{
foreach (ProcessModule m in process.Modules)
{
if (String.Compare(m.FileName, path, StringComparison.InvariantCultureIgnoreCase) == 0)
{
matchingProcesses.Add(process);
break;
}
}
}
matchingProcesses.ForEach(p => p.Kill());
编辑:更新了代码,以便将字符集敏感度考虑在内进行字符串比较。