编辑:这里的问题不是它是通过 GP 锁定的,而是它是在服务帐户下作为服务运行的,并且它无权访问交互式桌面
我有一个 C# 应用程序,需要检查用户会话何时被锁定,我正在使用Microsoft.Win32.SystemEvents.SessionSwitch
当用户手动锁定机器时,这可以正常工作。
问题是,当通过组策略(用户配置 > 策略 > 管理模板 > 个性化 > 屏幕保护程序超时)锁定计算机时,应用程序不会选择开关。
还有其他方法可以检查机器是否被锁定吗?或者是否有另一种方法可以通过应用程序选择的组策略来锁定计算机?
注意:该应用程序在 Windows 7 上作为具有完全管理权限的服务运行
这是我的代码,提前致谢! :)
public void OnStart(string[] args)
{
Microsoft.Win32.SystemEvents.SessionSwitch += new Microsoft.Win32.SessionSwitchEventHandler(SystemEvents_SessionSwitch);
}
void SystemEvents_SessionSwitch(object sender, Microsoft.Win32.SessionSwitchEventArgs e)
{
if (e.Reason == SessionSwitchReason.SessionLock)
{
//DO STUFF
}
}
我通过在 Windows 事件查看器中启用“其他登录/注销事件”并搜索锁定和解锁事件来解决此问题。
//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;
}
}
注意:这将检查所有事件日志,因此您需要添加一个限定符来确定您想要查看的过去多远。
如果您每十秒检查一次锁定/解锁会话,您只想处理一个EventRecord
如果是同一时期的话。
您可以访问eventlist.TimeCreated
做类似...的事情的价值
if (eventInstance.TimeCreated > DateTime.Now - TimeSpan.FromSeconds(10))
{
eventList.Add(eventInstance);
}
优雅吗?不,有效果吗?是的。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)