我正在尝试制作一个能够ping IP x次数的程序,然后将结果写入日志并通过电子邮件发送该日志。它工作正常,直到我决定添加时间戳。当我尝试使用额外的代码运行时间戳时,它使得控制台不会ping IP,但编写器会将时间戳写入文件,我不知道为什么。这是该子代码。
Sub ping(ByVal interval As Integer, ByVal ip As String, ByVal desktop As String, ByRef counter As Integer, ByRef boolping As Boolean)
Dim now As New DateTime
Dim writer As System.IO.StreamWriter = System.IO.File.AppendText(desktop & "\log.txt")
While counter > 0 And boolping = True
now = DateTime.Now
writer.WriteLine(now)
boolping = My.Computer.Network.Ping(ip)
Process.Start("CMD", "/c ping " & ip & " >> " & desktop & "\log.txt")
System.Threading.Thread.Sleep(interval)
counter = counter - 1
End While
writer.Close()
End Sub
答案 0 :(得分:0)
不确定您使用Process.Start
的原因?您已经在.NET框架中拥有完成此任务所需的一切。
以下是如何操作:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim address As String = "www.yahoo.com"
Dim log As String = "log.txt"
Dim count As Integer = 5
For i As Integer = 0 To count
Dim ping As Ping = New Ping()
Dim pingReply As PingReply = ping.Send(address)
Dim contents As String =
String.Format("{0}{1}{2}{3}{4}{5}",
DateTime.Now, vbTab, address,
vbTab, pingReply.Status, vbCrLf)
File.AppendAllText(log, contents)
Thread.Sleep(TimeSpan.FromSeconds(1))
Next
End Sub
结果:
10/2/2014 9:24:20 PM www.yahoo.com Success
10/2/2014 9:24:21 PM www.yahoo.com Success
10/2/2014 9:24:22 PM www.yahoo.com Success
10/2/2014 9:24:23 PM www.yahoo.com Success
10/2/2014 9:24:24 PM www.yahoo.com Success
10/2/2014 9:24:25 PM www.yahoo.com Success