Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting last reboot time [duplicate]

Tags:

date

c#

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);
like image 474
Kevin Zhou Avatar asked Jul 19 '12 14:07

Kevin Zhou


People also ask

How can I tell how long my last reboot was?

Task Manager Instead of showing the last boot time, it shows the uptime of the system since the last boot. To open Task Manager, simply use the Ctrl + Shift + Esc keyboard shortcut. Navigate to the Performance tab. The system uptime will be displayed near the bottom of the window.

How do you check when was Windows 10 remotely last rebooted?

You can use the systeminfo command to get the last boot time of the computer. In the above command, systeminfo uses /S parameter to specify the server name. It returns the last boot time for the remote computer.

When was my PC last rebooted?

Just enter eventvwr in the run dialog (which can be called by pressing Win + R ). Under Windows Logs > System look for events from the "Kernel-Power". This will also show if the system unexpectedly restarted by a blue screen and show events prior to it.


2 Answers

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());
        }
    }
}
like image 95
Eonasdan Avatar answered Oct 25 '22 07:10

Eonasdan


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/

like image 22
HatSoft Avatar answered Oct 25 '22 08:10

HatSoft