65.9K
CodeProject is changing. Read more.
Home

How to Get the Last Restart/Reboot Time for Windows

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.63/5 (8 votes)

Feb 12, 2008

CPOL
viewsIcon

54627

downloadIcon

693

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