Getting the last reboot time

Possible duplicate:
Build Date Display
How do I know when Windows was started or shut down?

for my purposes I am writing a C # executable that will calculate the time (minutes) difference from the point in time and time of the last server reboot.

I am currently collecting and analyzing the output from cmd β†’ "net stats server" and creating a new DateTime object, and then comparing it with DateTime.Now with a TimeSpan object.

Is there a cleaner way to do this without using third party downloads ? I am afraid that not all date formats from "net stats server" are in the format that I expect.

** edit my bad, this is a duplicate, but for what was worth my decision, it was this:

 float ticks = System.Environment.TickCount; Console.WriteLine("Time Difference (minutes): " + ticks / 1000 / 60); Console.WriteLine("Time Difference (hours): " + ticks / 1000 / 60 / 60); Console.WriteLine("Time Difference (days): " + ticks / 1000 / 60 / 60 / 24); 
+7
source share
2 answers

this answer should help you. If you want to know when the system was rebooted, just take the uptime value and subtract it from the current date / time

code from related answer

 public TimeSpan UpTime { get { using (var uptime = new PerformanceCounter("System", "System Up Time")) { uptime.NextValue(); //Call this an extra time before reading its value return TimeSpan.FromSeconds(uptime.NextValue()); } } } 
+11
source

you can do it with powsershell using wmi in it

 $wmi = Get-WmiObject -Class Win32_OperatingSystem -Computer "RemoteMachineName" $wmi.ConvertToDateTime($wmi.LastBootUpTime) 

Something like that...

A very useful link if you decide to go with this route and want to learn more about powershell using WMI http://www.powershellpro.com/powershell-tutorial-introduction/powershell-wmi-methods/

+1
source

All Articles