使用 vb.net 中的文件路径以编程方式终止应用程序

发布于 2024-08-04 06:02:54 字数 125 浏览 10 评论 0原文

我想通过 vb.net 使用完整文件路径终止应用程序,但在进程下找不到它。我希望有一个简单的 Process.Stop(filepath),就像 Process.Start 一样,但没有这样的运气。

我怎样才能这样做呢?

I want to terminate an application using the full file path via vb.net, yet I could not find it under Process. I was hoping for an easy Process.Stop(filepath), like with Process.Start, but no such luck.

How can I do so?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浊酒尽余欢 2024-08-11 06:02:54

您必须查看每个进程的“模块”属性,然后根据所需的路径检查文件名。

下面是一个示例:

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());

编辑:更新了代码以在字符串比较时考虑区分大小写。

You would have to look into each process' Modules property, and, in turn, check the filenames against your desired path.

Here's an example:

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());

EDIT: updated the code to take case sensitivity into account for string comparisons.

坠似风落 2024-08-11 06:02:54

尝试

System.Diagnostics.Process.GetProcessesByName(nameOfExeFile).First().Kill()

这会忽略文件的路径。

try

System.Diagnostics.Process.GetProcessesByName(nameOfExeFile).First().Kill()

This ignores the path of the file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文