首页 > 解决方案 > 当用户解锁 Windows 时,应用程序需要在 Windows 7 中执行一些任务

问题描述

当用户解锁 Windows 时,该应用程序需要在 Windows 7 中执行一些任务。它是一个普通的应用程序,而不是一个服务。我尝试过以下 操作-如何在 c# windows 应用程序中获取 windows 解锁事件?

此解决方案适用于 Windows 10,但不适用于 Windows 7。可以做什么?

标签: c#

解决方案


看到这个,windows 锁定这个事件你必须解锁这个 User Configuration > Policies > Administrative Templates > Personalization > Screen saver timeout 或者这样做:

   //Define strings for searching the eventlog.
    string lockEvent = "4800";
    string unlockEvent = "4801";

     //Define the Eventlog source you want (in this case it's Security)
    string LogSource = @"Security";

    //Add these together to make the full query for Lock and Unlock
    string LockQuery = " *[System/EventID=" + lockEvent + "]";
    string UnlockQuery = "*[System/EventID=" + unlockEvent + "]";


//Return true if there is any locked events found.

    private bool CheckForLock()
    {
        //Create Eventlog Reader and Query
        var elQuery = new EventLogQuery(LogSource, PathType.LogName, LockQuery);
        var elReader = new System.Diagnostics.Eventing.Reader.EventLogReader(elQuery);

        //Create a list of Eventlog records and add the found records to this
        List<EventRecord> eventList = new List<EventRecord>();
                for (EventRecord eventInstance = elReader.ReadEvent();
                    null != eventInstance; eventInstance = elReader.ReadEvent())
                {

                   eventlist.add(eventInstance);

                 }

          if(eventList.count > 0)
              {
                  return true;
              }
           else
             { 
                 return false;
             }

        }

来自C# SessionSwitchReason.SessionLock 当机器通过组策略锁定时不会触发


推荐阅读