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

Nick PriceTechnical ManagerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
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)?
0
Nick PriceTechnical ManagerAuthor Commented:

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

0
Nick PriceTechnical ManagerAuthor Commented:
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.

0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

käµfm³d 👽Commented:
Peculiar behavior indeed. The only other thing I can think to ask is: would it be plausible to post the full caller code?
0
Nick PriceTechnical ManagerAuthor Commented:
I've created a new project, and it is working now.  I have no clue why.

Regards

Nick



Dim process As New Process()

        process.StartInfo.WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.System)
        process.StartInfo.FileName = "netstat"
        process.StartInfo.Arguments = "-a -no"
        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()
        MsgBox(output)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Nick PriceTechnical ManagerAuthor Commented:
Awarded some points to kaufmed for help and support, even through no solutions was required in the end.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.