1

我有一个 C# 应用程序来查找用户的“开始工作”和“完成工作”事件。目标是获取包含日期时间值的列表,PC 何时“启动”以及何时再次“关闭”。

这适用于登录/注销和休眠,但不适用于待机 ( save energy)。搜索eventvwr我找不到连接到“进入待机”和“从待机中唤醒”的正确事件。

有了这个,我从 Windows 事件日志中读取:

public SortedDictionary<string, UserProfileEvent> ReadUserProfileEvents() {
    string queryString = string.Format("*[System[TimeCreated[@SystemTime>='{0}' and @SystemTime<='{1}']]]", this.StartDate.ToString("s"), this.EndDate.ToString("s"));
    var q = new EventLogQuery("Microsoft-Windows-User Profile Service/Operational", PathType.LogName, queryString);
    var r = new EventLogReader(q);

    var liste = new SortedDictionary<string, UserProfileEvent>();

    EventRecord e = r.ReadEvent();
    UserProfileEvent upe = null;
    while (e != null) {
        upe = new UserProfileEvent(e);
        try {
            liste.Add(upe.SortKey, upe);
        }
        catch (Exception exp) {
            throw new Exception("Some error text", exp);
        }
        e = r.ReadEvent();
    }
    return liste;
}

任何想法在哪里可以找到正确的事件?

编辑:我刚刚找到“Microsoft-Windows-Power-Troubleshooter”和“Microsoft-Windows-Kernel-Power”。这些协议似乎指向了正确的方向......

4

2 回答 2

2

并非所有内容都会在事件日志中列出,因为它们并不重要,需要写入日志(在磁盘上)(默认情况下)。

如果您的应用程序可以在后台运行,您可以订阅其中一些事件并做出相应的反应。正如“C Sharper”已经写过的,你可以在SystemEvents课堂上找到它们。

  • 待命(会话结束- 当用户尝试注销或关闭系统时发生。)
  • 用户锁定屏幕(会话切换- 当前登录用户发生变化时发生)
于 2015-11-02T11:24:24.920 回答
1

如果这是一个 Windows 窗体应用程序,您可以使用SystemEvents该类。

using System;
using Microsoft.Win32;

public sealed class App 
{
    static void Main() 
    {         
        // Set the SystemEvents class to receive event notification when a user 
        // preference changes, the palette changes, or when display settings change.
        SystemEvents.SessionEnding+= SystemEvents_SessionEnding;

        Console.WriteLine("This application is waiting for system events.");
        Console.WriteLine("Press <Enter> to terminate this application.");
        Console.ReadLine();
    }

    // This method is called when a user preference changes.
    static void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e) 
    {
       e.Category);
    }

}
于 2015-11-02T11:13:00.530 回答