4

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:

InvalidCastException was unhandled and highlights the line "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:

    Dim ProcID As String

VS2005 gives the error:

Value of type 'System.Diagnostics.Process' cannot be converted to 'String'.

I've tried declaring the Dim ProcID As Integer and got:

Value of type 'System.Diagnostics.Process' cannot be converted to '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.

Please help! This is driving me crazy!

Thanks

4

1 回答 1

2

以下更改将为您获取进程 ID 并将其放入文本框中。

 ProcID = Process.GetCurrentProcess.Id
 TextBox1.Text = ProcID.ToString

如果您有问题,请发表评论。

于 2013-11-14T18:34:39.873 回答