Click here to Skip to main content
15,890,897 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need the Following things in c#:-

1. Last system OFF Date and Time From event Viewer. Event ID = 1078
2. Last System OFF mode i.e Proper shut down / un expected (improper Shut Down) Event ID = 6006
3. Total System Running duration. Total Up Time.
All the above things are available in Event Viewer, but how can I get the only last record using WMI C#.


Regards
Posted
Updated 27-Jul-12 21:50pm
v2

1 solution

Hi,
try this code:
C#
private void button1_Click(object sender, EventArgs e)
{
   MessageBox.Show(GetLastSystemShutdown().ToString());
}

public static DateTime GetLastSystemShutdown()
{
   string sKey = @"System\CurrentControlSet\Control\Windows";
   Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(sKey);

   string sValueName = "ShutdownTime";
   byte[] val = (byte[])key.GetValue(sValueName);
   long valueAsLong = BitConverter.ToInt64(val, 0);
   return DateTime.FromFileTime(valueAsLong);
}
 
Share this answer
 
Comments
imrpk 24-Jul-12 5:29am    
Thanks for solution. But I also need improper shutdown date and time.......

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900