Module Module1 Sub Main() Dim str As String = "exit" Do Console.WriteLine("Ping命令测试工具(输入 exit ,回车,程序退出)") Console.WriteLine("请输入Ip地址:") str = Console.ReadLine If str <> "exit" Then Dim test As New PingTest(str) Console.WriteLine("Ping测试结果:" + test.ToString(test.cmdPingHost)) Console.WriteLine() End If Loop Until str = "exit" End Sub End Module Public Class PingTest Private host As String Public Enum ReturnValue Link '成功 DestHostUnreachable '无法到达目的主机 TimedOut '超时 UnKnownHost '无法解析主机 BadParameter '参数错误 End Enum Public Sub New() host = "127.0.0.1" End Sub Public Sub New(ByVal strIpaddress As String) host = strIpaddress End Sub Public Sub New(ByVal IpAddress As System.Net.IPAddress) host = IpAddress.ToString End Sub Public Function cmdPingHost() As ReturnValue Return cmdPingHost(host) End Function Public Function cmdPingHost(ByVal IpAddress As System.Net.IPAddress) Return cmdPingHost(IpAddress.ToString) End Function Public Function cmdPingHost(ByVal host As String) As ReturnValue Dim strRead As String '创建一个进程 Using p As New Process With p.StartInfo .FileName = "cmd.exe" .UseShellExecute = False .RedirectStandardInput = True .RedirectStandardOutput = True .RedirectStandardError = True .CreateNoWindow = True End With p.Start() p.StandardInput.WriteLine("ping -n 1 " & host) p.StandardInput.WriteLine("exit") strRead = p.StandardOutput.ReadToEnd p.Close() End Using '判断Ping结果 If strRead.IndexOf("(0% loss)") <> -1 Then Return ReturnValue.Link ElseIf strRead.IndexOf("Destination host unreachable.") <> -1 Then Return ReturnValue.DestHostUnreachable ElseIf strRead.IndexOf("Request timed out.") <> -1 Then Return ReturnValue.TimedOut ElseIf strRead.IndexOf("Unknown host") <> -1 Then Return ReturnValue.UnKnownHost Else Return ReturnValue.BadParameter End If End Function Public Overloads Function ToString(ByVal value As ReturnValue) As String Select Case value Case ReturnValue.Link : Return "成功" Case ReturnValue.DestHostUnreachable : Return "无法到达目的主机" Case ReturnValue.TimedOut : Return "超时" Case ReturnValue.UnKnownHost : Return "无法解析主机" Case ReturnValue.BadParameter : Return "参数错误" Case Else : Return "未知错误" End Select End Function End Class