问题
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);
回答1:
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());
}
}
}
回答2:
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/
来源:https://stackoverflow.com/questions/11562699/getting-last-reboot-time