如何找到开放程序的PID

时间:2016-10-30 12:25:50

标签: vb.net process pid

我试图弄清楚如何找到PID,例如notepad.exe。我用谷歌搜索了这个,我发现了很多东西,但我无法解决它。我试过这段代码:

Dim currentProcess As Process = Process.GetCurrentProcess()
Dim localAll As Process() = Process.GetProcesses()
Dim localByName As Process() = Process.GetProcessesByName("notepad")
Label1.Text = localByName.ToString 

但执行后,我会在system.diagnostics.process[]中获得 label1 作为输出。我仍然在vb.net中相当noob,并且似乎无法在这里找到问题。

1 个答案:

答案 0 :(得分:1)

GetProcessesByName返回一个数组。您可能有多个Notepad正在运行,但以下是您获取第一个ID的方法。

Dim currentProcess As Process = Process.GetCurrentProcess()
Dim localAll As Process() = Process.GetProcesses()
Dim localByName As Process() = Process.GetProcessesByName("notepad")

'Do this
Label1.Text = localByName(0).Id

'Find all!
for each proc in localByName
   Label1.Text &= proc.Id & vbCrLf 'vbCrlf just adds a new line for reading purposes
next