Get how long the computer has been running.

This function gives the programmer the ability to get a string representation of how long the computer has been running since the OS has started. The TickCount function is a value that has existed in the Windows API since Windows 2000.

VB.NET

''' <summary>
''' Get a human readable string representation of the 
''' amount of time the computer has been on since the OS started.
''' </summary>
''' <returns>How long the computer has been running in days, hours
''' minutes and seconds.
''' </returns>
''' <remarks></remarks>

Public Function getUptime() As String
    Dim strResult As String = String.Empty
    strResult += Math.Round(Environment.TickCount / 86400000) & " days, "
    strResult += Math.Round(Environment.TickCount / 3600000 Mod 24) & " hours, "
    strResult += Math.Round(Environment.TickCount / 120000 Mod 60) & " minutes, "
    strResult += Math.Round(Environment.TickCount / 1000 Mod 60) & " seconds."
    Return strResult
End Function

' Example usage of this function.
MessageBox.Show(getUptime)

See Also

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License