如何在 VB.NET 中获取正在运行的进程名称列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11055147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get list of Process Names running, in VB.NET?
提问by Thalia
I am trying to find out if an instance of an application (not vb.net) is already running - because I will want to start it but I don't want to start it if it is already running. I have found a solution to check if a process is running:
我试图找出应用程序(不是 vb.net)的实例是否已经在运行 - 因为我想启动它,但如果它已经在运行,我不想启动它。我找到了一个检查进程是否正在运行的解决方案:
Dim proc As Integer = Process.GetProcessesByName(ProcessName).GetUpperBound(0) + 1
and return True if >=1 (or just the process number).
如果 >=1(或只是进程号),则返回 True。
My problem is, this is a third-party application, and its process name is not just a name but it contains a version number (which I may not know at run time), and it also seems to add a *32 (so probably a *64 if it is installed in x64 ?).
我的问题是,这是一个第三方应用程序,它的进程名称不仅仅是一个名称,还包含一个版本号(我可能在运行时不知道),而且它似乎还添加了一个 *32(所以可能如果安装在 x64 中,则为 *64 ?)。
I need to get a list of running processes, by name, and test if "processname" is a substring of the name. But I haven't been successful in getting a list of names, only process id's.
我需要按名称获取正在运行的进程列表,并测试“processname”是否是名称的子字符串。但是我没有成功获得名称列表,只有进程 ID。
采纳答案by Reed Copsey
I need to get a list of running processes, by name, and test if "processname" is a substring of the name.
我需要按名称获取正在运行的进程列表,并测试“processname”是否是名称的子字符串。
You could use:
你可以使用:
Dim procExists as Boolean = Process.GetProcesses().Any(Function(p) p.Name.Contains(processName))
This will look through all of the processes, and set the procExists
value to True if any process which containsprocessName
exists in the currently executing processes. This should handle the existence of the unknown version number as well as the *32
that may occur if you're running on a 64bit OS (that's the WOW64 flag saying that it's a 32bit process running on a 64bit OS).
这将查看所有进程,procExists
如果当前正在执行的进程中processName
存在包含的任何进程,则将值设置为 True 。这应该处理未知版本号的存在以及*32
如果您在 64 位操作系统上运行时可能发生的问题(WOW64 标志表示它是在 64 位操作系统上运行的 32 位进程)。
回答by John Koerner
You can loop through the running processes like this:
您可以像这样循环运行正在运行的进程:
For Each p As Process In Process.GetProcesses()
Debug.WriteLine(p.ProcessName)
Next
回答by nnm
another way:
其它的办法:
Dim psList() As Process
Try
psList = Process.GetProcesses()
For Each p As Process In psList
Console.WriteLine(p.Id.ToString() + " " + p.ProcessName)
Next p
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
Console.ReadKey()
回答by EnriqueBet
Just tried the answer posted of Reed Copesy and this seems to be changed, what worked for me is:
刚刚尝试了 Reed Copesy 发布的答案,这似乎有所改变,对我有用的是:
Dim procExists as Boolean = Process.GetProcesses().Any(Function(p) p.ProcessName.Contains(processName))
Also is possible to retrieve the array by name and check directly if is contained on the process array:
也可以按名称检索数组并直接检查是否包含在进程数组中:
Process.GetProcessesByName(processName).Length > 0
Thanks!
谢谢!