2015-11-02 35 views
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」。這些協議似乎指向正確的方向...

回答

2

並非一切都將在事件日誌上市,因爲他們都不是很重要的一個日誌(在磁盤上)寫將需要(默認)。

如果您的應用程序可以在後臺運行,您可以訂閱其中的一些事件並作出相應的反應。正如「C Sharper」已經寫過的,你可以在SystemEvents class找到它們。

  • 去袖手旁觀 - ( - 發生現在當登錄的用戶已經改變Session switch
  • 用戶鎖定屏幕(Session ending發生當用戶試圖註銷或關閉系統。)
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); 
    } 

} 
+0

但是我不想對事件做出反應,我想在過去的幾個星期裏得到一個「開始活躍」和「結束活躍」的列表。 – Shnugo

相關問題