我试图弄清楚如何找到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,并且似乎无法在这里找到问题。
答案 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