You can use the System.Diagnostics
and System.Management
namespaces to fetch system information including the operating system and its boot time. Here's an example code snippet that prints the system boot time as a DateTime
object:
main.cs738 chars23 lines
The above code works by querying the Win32_OperatingSystem
WMI class for information about the primary operating system on the machine, and then reading its LastBootUpTime
property which holds the time when the operating system was last booted up. Note that the LastBootUpTime
field is in WMI datetime format, so we need to convert it back to a DateTime
object to make it human-readable.
gistlibby LogSnag