ユーザーがウィンドウにログイン、ログアウト、ロック、ロック解除された時間を取得する方法は?


または、Windows をスリープ状態にするか、Windows (コンピューター) を起動します。

昨日いつ寝たのか、今日いつ起きたのかを知りたい場合、この C# .NET WinForms .exe ファイルをチェックインすると、そのような最後のイベントのテーブルが表示されます。

イベントビューアのデータを取得することでこれを始めたことがありますが、残念ながらコードを失いました。

私が試したこと:

Windows 10 / 11 ユーザーのログイン履歴を検索または確認する方法は?[^]

void refresh()
        {
            dataGridViewMain.Rows.Clear();


            EventLog log = new EventLog("Security");
            var entries = log.Entries.Cast<EventLogEntry>()
                                     .Where(x => x.InstanceId == 4624)
                                     .Select(x => new
                                     {
                                         x.MachineName,
                                         x.Site,
                                         x.Source,
                                         x.Message,
                                         x.TimeGenerated,
                                         
                                     }).ToList();





            foreach (var evl in entries)
            {
                dataGridViewMain.Rows.Add(new object[] { "Log in", evl.TimeGenerated });
            }












            //log = new EventLog("Security");
            entries = log.Entries.Cast<EventLogEntry>()
                                     .Where(x => x.InstanceId == 2625)
                                     .Select(x => new
                                     {
                                         x.MachineName,
                                         x.Site,
                                         x.Source,
                                         x.Message,
                                         x.TimeGenerated,

                                     }).ToList();


            foreach (var evl in entries)
            {
                dataGridViewMain.Rows.Add(new object[] { "Log out", evl.TimeGenerated });
            }














            //log = new EventLog("Security");
            entries = log.Entries.Cast<EventLogEntry>()
                                     .Where(x => x.InstanceId == 4800)
                                     .Select(x => new
                                     {
                                         x.MachineName,
                                         x.Site,
                                         x.Source,
                                         x.Message,
                                         x.TimeGenerated,

                                     }).ToList();


            foreach (var evl in entries)
            {
                dataGridViewMain.Rows.Add(new object[] { "Lock", evl.TimeGenerated });
            }








            //log = new EventLog("Security");
            entries = log.Entries.Cast<EventLogEntry>()
                                     .Where(x => x.InstanceId == 4801)
                                     .Select(x => new
                                     {
                                         x.MachineName,
                                         x.Site,
                                         x.Source,
                                         x.Message,
                                         x.TimeGenerated,

                                     }).ToList();


            foreach (var evl in entries)
            {
                dataGridViewMain.Rows.Add(new object[] { "Unlock", evl.TimeGenerated });
            }







            dataGridViewMain.Sort(dataGridViewMain.Columns[1],ListSortDirection.Descending);


        }

DataGridView で「ログイン」イベントしか取得していません。他のイベントを取得できないのはなぜですか? 他の 3 つのイベント ID 番号が正しいか確認していただけますか?

解決策 1

これは、いくつかの実用的な解決策を含むGoogle検索です。 c# イベント ビューアー ログの読み取り – Google 検索[^]



Source link

コメント

タイトルとURLをコピーしました