Public Class Form1
Dim sf() As String = {"百度", "新浪", "搜狐", "卡巴斯基"}
Dim sfs() As String = {"www.baidu.com", "www.sina.com.cn", "www.sohu.com", "www.kaspersky.com"}
Dim time(sf.Length) As Integer
Dim loops As Integer = 0
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For i As Integer = 0 To sf.Length - 1
time(i) = pingClient.Send(sfs(i)).RoundtripTime
Next
Init_ListView()
End Sub
Dim WithEvents pingClient As New Ping()
Private Sub Init_ListView()
With ListView1
Dim wi As Integer = .Width / 4
.Columns.Add("服务器名称", wi, HorizontalAlignment.Center)
.Columns.Add("速度", wi, HorizontalAlignment.Center)
.Columns.Add("状态", wi, HorizontalAlignment.Center)
.Columns.Add("其它", wi, HorizontalAlignment.Center)
.View = View.Details
End With
Dim lvi As ListViewItem
For i As Integer = 0 To sf.Length - 1
lvi = New ListViewItem(sf(i))
If time(i) < 20 Then
lvi.SubItems(0).ForeColor = Color.Green
ElseIf time(i) < 50 Then
lvi.SubItems(0).ForeColor = Color.Yellow
Else
lvi.SubItems(0).ForeColor = Color.Red
End If
If time(i) < 20 Then
lvi.SubItems.Add("速度优")
ElseIf time(i) < 50 Then
lvi.SubItems.Add("速度不错")
ElseIf time(i) < 100 Then
lvi.SubItems.Add("速度有点差了")
Else
lvi.SubItems.Add("玩不了网游")
End If
lvi.SubItems.Add(time(i))
ListView1.Items.Add(lvi)
Next
End Sub
End Class
Public Function CmdExcute(ByVal ExcuteCommand As String, ByRef ErrorMessage As String) As String
Dim strRtn As String ' 返回值
Dim myProcess As System.Diagnostics.Process = New System.Diagnostics.Process
Dim bOk As Boolean
myProcess.StartInfo.FileName = "cmd"
myProcess.StartInfo.UseShellExecute = False
myProcess.StartInfo.RedirectStandardInput = True
myProcess.StartInfo.RedirectStandardOutput = True
myProcess.StartInfo.RedirectStandardError = True
myProcess.StartInfo.CreateNoWindow = True
Try
myProcess.Start()
myProcess.StandardInput.WriteLine(ExcuteCommand)
myProcess.StandardInput.WriteLine("exit")
ErrorMessage = myProcess.StandardError.ReadToEnd()
If ErrorMessage = "" Then
strRtn = myProcess.StandardOutput.ReadToEnd()
End If
myProcess.WaitForExit()
myProcess.Close()
Catch ex As Exception
sReturn = ex.Message
End Try
myProcess.Dispose()
Return strRtn
End Function
Dim strRtn As String
Dim strError As String
strRtn = CmdExcute("ping www.163.com", strError)
If strError = "" Then
Dim intPos As Integer
Dim strFindString As String
strFindString = "Average = "
intPos = strRtn.IndexOf(strFindString)
If intPos > 0 Then
strRtn = strRtn.Substring(intPos + strFindString.Length)
End If
intPos = strRtn.IndexOf("ms")
If intPos > 0 Then
strRtn = strRtn.Substring(0, intPos)
End If
End If
MsgBox(strRtn)