Link to home
Start Free TrialLog in
Avatar of Nick Price
Nick PriceFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Netstat with PID using VB.net

Found this code on Experts Exchange, but can't get it to work.
When the code hits the process.Start, another copy of my application opens, and netstat doesn't execute. Everything I try again ( using command button) another copy of my app loads.
Something silly happening here. but what?


Dim process As New Process()
 process.StartInfo.FileName = "netstat.exe"
  If includeProcess Then
            process.StartInfo.Arguments = "-b"
   End If
    process.StartInfo.CreateNoWindow = True
    process.StartInfo.ErrorDialog = False
    process.StartInfo.RedirectStandardOutput = True
    process.StartInfo.UseShellExecute = False
    process.Start()
Imports System
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.Text.RegularExpressions
 
Public Class ActiveConnection
    Public Protocol As String = ""
    Public LocalAddress As String = ""
    Public ForeignAddress As String = ""
    Public State As String = ""
    Public ProcessID As Integer
    Public ProcessExecutable As String = ""
End Class
 
Public Class NetStat
 
    Public Shared Function GetActiveConnections(ByVal includeProcess As Boolean) As List(Of ActiveConnection)
 
        Dim process As New Process()
 
        process.StartInfo.FileName = "netstat.exe"
 
        If includeProcess Then
            process.StartInfo.Arguments = "-b"
        End If
 
        process.StartInfo.CreateNoWindow = True
        process.StartInfo.ErrorDialog = False
        process.StartInfo.RedirectStandardOutput = True
        process.StartInfo.UseShellExecute = False
 
        process.Start()
 
        process.WaitForExit(5000)
 
        Dim output As String = process.StandardOutput.ReadToEnd()
 
        Return ParseConnections(output)
 
    End Function
 
    Private Shared Function ParseConnections(ByVal input As String) As List(Of ActiveConnection)
 
        Dim list As New List(Of ActiveConnection)
 
        Dim allLines() As String = input.Replace(vbCr, "").Split(Chr(10))
 
        Dim connection As ActiveConnection = Nothing
        For Each line As String In allLines
 
            If line.Trim().Length > 0 Then
                If ParseActiveConnection(line.Trim(), connection) Then
                    list.Add(connection)
                End If
            End If
 
        Next line
 
        Return list
 
    End Function
 
    Private Shared Function ParseActiveConnection(ByVal input As String, ByRef connection As ActiveConnection) As Boolean
 
        Dim patternSpaces As String = "(\s+)"
        Dim patternProtocol As String = "(?<proto>[A-Z\d]+)"
        Dim patternLocal As String = "(?<local>[A-Za-z\d:_.]+)"
        Dim patternForeign As String = "(?<foreign>[A-Za-z\d:_.]+)"
        Dim patternState As String = "(?<state>[A-Z_\d]+)"
        Dim patternPid As String = "?(?<pid>\d+)?"
 
        Dim pattern As String = patternProtocol & patternSpaces & _
         patternLocal & patternSpaces & patternForeign & _
         patternSpaces & patternState & patternSpaces & patternPid
 
        Dim addConnection As Boolean = False
 
        Dim matchConnection As Match = Regex.Match(input, pattern)
 
        If matchConnection.Success Then
            connection = New ActiveConnection
 
            connection.Protocol = matchConnection.Groups("proto").Value
            connection.LocalAddress = matchConnection.Groups("local").Value
            connection.ForeignAddress = matchConnection.Groups("foreign").Value
            connection.State = matchConnection.Groups("state").Value
            connection.ProcessID = CInt(matchConnection.Groups("pid").Value)
 
            addConnection = True
 
        Else
            Dim patternExe As String = "\[(?<exe>[A-Za-z\d.]+)\]"
            Dim matchExe As Match = Regex.Match(input, patternExe)
 
            If matchExe.Success Then
                connection.ProcessExecutable = matchExe.Groups("exe").Value
            End If
        End If
 
        Return addConnection
 
    End Function
 
End Class

Toggle HighlightingOpen in New WindowSelect All

Open in new window

Avatar of kaufmed
kaufmed
Flag of United States of America image

Hmmm...  your code seems to work fine form me as is. What does the code that executes the code above look like (i.e. who calls this)?
Avatar of Nick Price

ASKER


I've tried then process start on it's own, without the GetActiveConnections, and it does the same.
I've unable to get the netstat to kick off, and my application just opens another instance of itself.
This is how i'm calling it. The try doesn't fail.

Cheers Nick

    Try
            Dim Xcoll = NetStat.GetActiveConnections(False)
        Catch ex As Exception

        End Try

I've just replaced the "netstat" with "notepad" and it opens notepad fine, and doesn't open another instance of my app. Something to do with netstat.

SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Awarded some points to kaufmed for help and support, even through no solutions was required in the end.