Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 如何在Vb.net中将(字节)数据流转换为字符串_String_Vb.net_Sockets_Netstream - Fatal编程技术网

String 如何在Vb.net中将(字节)数据流转换为字符串

String 如何在Vb.net中将(字节)数据流转换为字符串,string,vb.net,sockets,netstream,String,Vb.net,Sockets,Netstream,我的程序必须使用套接字编程从服务器读取数据,以终止远程pc中的进程。 从套接字连接转换netstream后,我将流转换为字符串,但我无法终止进程,因为输入字符串未转换为字符串。 我很困惑,因为我能够使用字符串方法,比如.remove(),但是当我将输入变量传递给函数killProcess时,processess并没有被删除 'socket datastream If NetStream.DataAvailable Then Dim bytes(TCPClient.Receive

我的程序必须使用套接字编程从服务器读取数据,以终止远程pc中的进程。 从套接字连接转换netstream后,我将流转换为字符串,但我无法终止进程,因为输入字符串未转换为字符串。 我很困惑,因为我能够使用字符串方法,比如.remove(),但是当我将输入变量传递给函数killProcess时,processess并没有被删除

'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

因为PID不是名字?您需要
Process.GetProcessById()
。。。我也不太明白网络部分在这里是如何相关的NET可能试图告诉您有问题,但您正在接受任何例外我不知道您发送的是什么,但很可能是这样的:
“杀死记事本”
对吗?如果随后使用
.Remove(0,4)
对其执行操作,结果将是:
“记事本”
。注意到什么了吗?开头有一个空格。你可以通过使用
input.Trim()
@MrPaulch摆脱这个问题。他们代码中的注释是关于PID的,所以我猜他们会发送类似“kill 123”的消息。他们必须将该字符串解析为int,这里有很多问题已经解决了。从方法上面的注释判断,他正在发送进程名。参数的名称确实有误导性。问题也是如此。这个答案应该是什么意思?如果任务管理器正在运行,它将杀死记事本的所有实例。我看不出这是如何回答OP的问题的。
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