9

Possible Duplicate:
Displaying the build date
How to know when was Windows started or shutdown?

for my purposes I am writing a C# executable that will calculate the difference in time (minutes) from the time right now and the time the server was last rebooted.

What I am currently doing now is capturing and parsing the output from cmd -> "net stats server" and creating a new DateTime object then comparing that with DateTime.Now with a TimeSpan object.

Is there a cleaner way to do this without the use of 3rd party downloads? I am scared that not all date formats from "net stats server" are in the format that I will expect.

**edit my bad, this is a duplicate, but for what it is worth my solution was using 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);
2
  • 4
    when you reboot just look at your watch
    – Timmerz
    Commented Jul 19, 2012 at 14:14
  • works for scheduled reboots =) but if server suddenly crashes and starts up, I need to perform actions iff this reboot was "unscheduled"
    – Kevin Zhou
    Commented Jul 19, 2012 at 14:19

2 Answers 2

14

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

code from linked 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());
        }
    }
}
0

you can do this with powsershell using the WMI in it

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

Something like this...

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.