3

我知道我可以得到自上次启动以来的时间,Environment.TickCount
但是是否有可能得到计算机从休眠或睡眠中唤醒的最后时间?

(请不要使用 WMI)

4

3 回答 3

7

试试这个命令 - 使用Process来启动它 - 你需要解析结果

cmd /k wevtutil qe System /q:"*[System[Provider[@Name='Microsoft-Windows-Power-Troubleshooter']]]" /rd:true /c:1 /f:text

如果您想了解更多信息,请从这里提取...

于 2012-07-24T09:56:29.113 回答
0

根据此页面,您必须收听PBT_APMRESUMEAUTOMATIC(如果您想知道用户是否是唤醒的“原因”,则必须收听 PBT_APMRESUMESUSPEND)

我认为Microsoft.Win32.SystemEvents ( PowerModeChanged event ) 是值得一看的地方,但是,如果没有进一步调查,可能会有一些问题

这个页面可能会让你开始感兴趣。

于 2012-07-24T09:01:30.550 回答
0

尽管上面有一个提供核心查询的答案,但对于那些决定想要更完整的东西的人来说,这里有一个更充实的解决方案。

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;
}

用法:

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);
}

将需要以下内容;

using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Xml;
using System.Windows.Forms; // Needed only if you want to show the messagebox

在此示例中,我试图查找 PC 唤醒时间(如中所列@Name='WakeTime'- 更改以获取唤醒时间或查询返回的另一个单独值)。例如,如果您想知道是什么唤醒了 PC,请WakeSourceText改用。

您肯定会想要添加一些错误检查,但这应该可以实现您想要的。

于 2019-11-12T10:25:31.587 回答