vb.net 如何在我使用该 VB 窗体启动的进程的 VB 窗体中获取进程 ID?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19985319/
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 the Process ID within a VB Form of a Process I Launched with that VB Form?
提问by user2993272
I'm using a form I made in VB2005 to open a program upon button press and then in a text field display the Process ID (again upon button press). When I run it the form will open the program (Notepad.exe) but when I click the button to view the Process ID Visual Studio 2005 says:
我正在使用我在 VB2005 中制作的表单在按下按钮时打开程序,然后在文本字段中显示进程 ID(再次按下按钮时)。当我运行它时,表单将打开程序 (Notepad.exe) 但当我单击按钮查看进程 ID Visual Studio 2005 时说:
InvalidCastException was unhandled and highlights the line "TextBox1.Text = ProcID"
InvalidCastException 未处理并突出显示“TextBox1.Text = ProcID”行
Imports System
Imports System.Diagnostics
Public Class Form1
Dim myProcess As New Process
Dim ProcID
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Launch.Click
myProcess.StartInfo.FileName = "notepad.exe"
myProcess.Start()
End Sub
Private Sub GetID_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GetID.Click
ProcID = Process.GetCurrentProcess()
TextBox1.Text = ProcID
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
When I try to change the ProcID declaration into a string by:
当我尝试通过以下方式将 ProcID 声明更改为字符串时:
Dim ProcID As String
VS2005 gives the error:
VS2005 报错:
Value of type 'System.Diagnostics.Process' cannot be converted to 'String'.
“System.Diagnostics.Process”类型的值无法转换为“String”。
I've tried declaring the Dim ProcID As Integer and got:
我试过将 Dim ProcID 声明为整数并得到:
Value of type 'System.Diagnostics.Process' cannot be converted to 'Integer'.
“System.Diagnostics.Process”类型的值无法转换为“Integer”。
I've also tried the following change with no luck:
我也尝试了以下更改但没有运气:
Private Sub GetID_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GetID.Click
ProcID = Process.GetCurrentProcess(myProcess)
TextBox1.Text = CInt(ProcID)
End Sub
That error says:
该错误说:
Class 'System.Diagnostics.Process' cannot be indexed because it has no default property.
类“System.Diagnostics.Process”不能被索引,因为它没有默认属性。
Please help! This is driving me crazy!
请帮忙!这真让我抓狂!
Thanks
谢谢
采纳答案by FraserOfSmeg
The following change will get you the process ID and put it into the textbox.
以下更改将为您提供进程 ID 并将其放入文本框中。
ProcID = Process.GetCurrentProcess.Id
TextBox1.Text = ProcID.ToString
Comment if you have issues.
如果您有问题,请发表评论。

