VB .Net DateTime.Now

QuaziBee

Daemon Poster
Messages
661
Location
Calgary, AB
Writing a simple program to ping a PC, return a time stamp and status, and write it to a log file.
My output from the below code has the same exact timestamp for every entry. (pic)
rKrnz04.png

What am I doing wrong?

Code:
Imports System
Imports System.Net
Imports System.IO

Module Module1

    Sub Main()
        log()
        Console.WriteLine("Enter the IP Address of the computer to keep a log of.")
        Dim computer = Console.ReadLine
        Dim time = DateTime.Now
        Dim Success As String = "UP as of " & time & vbCrLf
        Dim Failure As String = "DOWN as of " & time & vbCrLf

        Do
            If ping(computer) = True Then
                Console.WriteLine(Success)
                appendLog(Success)
            Else
                Console.WriteLine(Failure)
                appendLog(Failure)
            End If
            System.Threading.Thread.Sleep(6000)
        Loop
    End Sub

    Sub appendLog(ByVal status)
        File.AppendAllText("C:\PingLog.txt", status)
    End Sub

    Sub log()
        Dim time = DateTime.Now
        If File.Exists("C:\PingLog.txt") = False Then
            File.AppendAllText("C:\PingLog.txt", "File Created At " & time & vbCrLf)
            Console.WriteLine("Log File Created At C:\PingLog.txt")
        Else
            File.AppendAllText("C:\PingLog.txt", vbCrLf & "Logging restarted at " & time & vbCrLf)
            Console.WriteLine("Log File Found. Appending To C:\PingLog.txt")
        End If
    End Sub

    Function ping(ByVal computer)
        Dim status As Boolean
        If My.Computer.Network.Ping(computer) Then
            status = True
        Else
            status = False
        End If
        Return status
    End Function
End Module
 
Move the call to DateTime.Now and the assignment of the Success * Failure strings into your Do...Loop.
 
Last edited:
Perfect, I see now that by defining it outside the loop it only calls the time once, instead of exiting the loop to call again.

Putting the call inside the loop is exactly what I needed.
Thanks mate!
 
Back
Top Bottom