Click here to Skip to main content
Licence CPOL
First Posted 12 Feb 2008
Views 20,697
Downloads 175
Bookmarked 15 times

How to Get the Last Restart/Reboot Time for Windows

By | 12 Feb 2008 | Article
This code snippet helps to get the last Windows reboot time using C# and interop

Introduction

This code snippet helps to get the time when the Windows OS was last rebooted, using C# code.

Background

It was required for me to get the last restart time for Windows in my program. But unfortunately I couldn't find any direct APIs from .NET. Also some articles suggested using the NetStat command and parsing the output as the available solution.

Using the Code

The following code snippet in C# will be helpful in getting the last restart time for Windows.

IntPtr bufPtr = IntPtr.Zero;
int val = NetStatisticsGet(null, "LanmanWorkstation", 0, 0, out bufPtr);
STAT_WORKSTATION_0 wks = new STAT_WORKSTATION_0();
if (val == 0)
{
    wks = (STAT_WORKSTATION_0)Marshal.PtrToStructure(bufPtr, typeof(STAT_WORKSTATION_0));
}
DateTime aRebootTime = DateTime.FromFileTime(wks.StatisticsStartTime);
System.Console.WriteLine(aRebootTime);       

Points of Interest

The current code shows how to get the last restart time for the workstation. It is also possible get the restart time from the server.

History

  • 12th February, 2008: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Gautham Jayaraman

Software Developer
Working for an MNC
India India

Member

Working for an MNC for last 5 years. Currently in application development, mainly ASP.NET. Has also worked with internet security and penetration testing. MCSD .NET certified. Enjoys travelling and food.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralLast Reboot Time PinmemberMember 39685016:41 2 Jan '09  
QuestionCan you explain how it works? Pinmemberadaiye20:14 19 Feb '08  
GeneralAnother way... PinmemberVDJ22:31 18 Feb '08  
GeneralSystem.Environment.TickCount PinmemberMember 206208816:56 18 Feb '08  
GeneralRe: System.Environment.TickCount PinmemberPredrag Tomasevic12:09 23 Nov '11  
GeneralSecurity Event Log PinmemberPeterTheGreat4:40 12 Feb '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 12 Feb 2008
Article Copyright 2008 by Gautham Jayaraman
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid