我的程序必须使用套接字编程从服务器读取数据以终止远程PC中的进程。 在我从套接字连接转换netstream后,我将流转换为String但我无法终止进程,因为输入字符串未转换为字符串。 我很困惑,因为我能够使用字符串方法,如.remove(),但是当我将输入变量传递给函数killProcess时,不会删除processsess。
'socket datastream
If NetStream.DataAvailable Then
Dim bytes(TCPClient.ReceiveBufferSize) As Byte
Dim input As String
NetStream.Read(bytes, 0, CInt(TcpClient.ReceiveBufferSize))
lblConnectStatus.Text = ("Server was disconnected ")
input = Encoding.ASCII.GetString(bytes)
If input.StartsWith("kill") Then
input = input.Remove(0, 4)
Try
KillProcess(input)
Catch ex As Exception
End Try
End if
End if
'kill process function
'this function works when i write the program name manually eg.:"notepad"
'but it doesn't work when i pass the parameter pid for the killProcess() function
Private Sub KillProcess(ByVal pid As String)
For Each P As Process In System.Diagnostics.Process.GetProcessesByName(pid)
P.Kill()
Next
End Sub
答案 0 :(得分:-2)
Dim p() As Process
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
p = Process.GetProcessesByName("taskmgr")
If p.Count > 0 Then
Dim pList() As System.Diagnostics.Process = System.Diagnostics.Process.GetProcessesByName("notepad")
For Each proc As System.Diagnostics.Process In pList
proc.Kill()
Next
End If
End Sub