How to convert a (byte) data stream into string in Vb.net

521 Views Asked by At

My program has to read a data from a server using socket programming to kill processes in a remote pc. After i convert the netstream from the socket connection, I convert the stream into String but I'm unable to kill the processes because the input string is not converted to string. I'm puzzled since I'm able to use string methods such as .remove(), but when i pass the input variable to the function killProcess the processess is not deleted.

'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
1

There are 1 best solutions below

1
Geared Silently On
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