Monday, December 21, 2009

How to Get the date-time of last windows shutdown using .NET

The solution is below, DateTime can be achieved  from a byte array with fewer statements using the BitConverter.The following  code will give the correct DateTime from the registry

 public static DateTime GetLastShutdownDate()

    string rKey = @"System\CurrentControlSet\Control\Windows";
    Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(rKey);

    string rValueName = "ShutdownTime";
    byte[] val = (byte[]) key.GetValue(rValueName);
    long AsLongValue= BitConverter.ToInt64(val, 0);
    return DateTime.FromFileTime(AsLongValue);

No comments:

Post a Comment