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
ASKER
ASKER
ASKER
Visual Basic .NET (VB.NET) is an object-oriented programming language implemented on the .NET framework, but also supported on other platforms such as Mono and Silverlight. Microsoft launched VB.NET as the successor to the Visual Basic language. Though it is similar in syntax to Visual Basic pre-2002, it is not the same technology,
TRUSTED BY