Reputation: 6506
I know I can get the time since the last boot using Environment.TickCount
But is it possible to get the last time a compute woke up from hibernation or sleep?
(Without using WMI please)
Upvotes: 4
Views: 2410
Reputation:
Although there's an answer above that provides the core query, here's a more fleshed out solution for anyone who decides they want something more complete.
private string getLastWakeInfo() {
String args = @"qe System /q:"" *[System[Provider[@Name = 'Microsoft-Windows-Power-Troubleshooter']]]"" /rd:true /c:1 /f:xml";
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "wevtutil.exe";
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.RedirectStandardInput = true;
psi.Arguments = args;
Process proc = Process.Start(psi);
proc.WaitForExit(2000);
String strOutput = proc.StandardOutput.ReadToEnd();
return strOutput;
}
Usage:
private void button1_Click_2(object sender, EventArgs e) {
String xml = getLastWakeInfo();
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
String path = "//*[local-name()='Event']/*[local-name()='EventData']/*[local-name()='Data'][@Name='WakeTime']";
XmlNode root = doc.DocumentElement;
XmlNode node = root.SelectSingleNode(path);
MessageBox.Show(node.InnerText);
}
The following will be needed;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Xml;
using System.Windows.Forms; // Needed only if you want to show the messagebox
In this example, I'm trying to find the PC wake time (as listed in @Name='WakeTime'
- Change that to get the wake time or another individual value that's returned from the query). For example, if you want to know what woke up the PC, use WakeSourceText
instead.
You'll definitely want to add some error checking, but this should achieve what you're looking for.
Upvotes: 0
Reputation: 24372
Try this command - use Process to fire it off - you'll need to parse the result
cmd /k wevtutil qe System /q:"*[System[Provider[@Name='Microsoft-Windows-Power-Troubleshooter']]]" /rd:true /c:1 /f:text
Lifted from here if you want more info...
Upvotes: 8
Reputation: 8821
According to this page you'll have to listen for PBT_APMRESUMEAUTOMATIC (and PBT_APMRESUMESUSPEND if you want to know if a user was the 'cause' for the wakeup).
I think Microsoft.Win32.SystemEvents (PowerModeChanged event) is the place to look but, without investigating any further, there might be some issues.
This page might be interesting to get you started.
Upvotes: -1