【问题标题】:monitor user activities using c# [duplicate]使用c#监视用户活动[重复]
【发布时间】:2014-06-25 08:24:23
【问题描述】:
我正在开发一个监控用户活动的应用程序。我需要知道每个用户的登录、注销、锁定和解锁时间。如何使用 C# 从本地计算机获取这些信息。
请注意:我不是在询问第一次登录和最后一次注销的时间。我需要整个用户活动,包括 Windows 锁定和解锁。那么只有我才能知道实际的工作时间。请让我知道是否有任何方法可以做到这一点
【问题讨论】:
标签:
c#
windows
wcf
monitoring
【解决方案1】:
所以这可以通过检查 SystemEvent_SessionSwitch 来完成,这段代码会告诉你何时有人离开办公桌并回到他们的办公桌。 (Timoty Carter写的代码示例)
Microsoft.Win32.SystemEvents.SessionSwitch += new Microsoft.Win32.SessionSwitchEventHandler(SystemEvents_SessionSwitch);
void SystemEvents_SessionSwitch(object sender, Microsoft.Win32.SessionSwitchEventArgs e)
{
if (e.Reason == SessionSwitchReason.SessionLock)
{
//I left my desk
}
else if (e.Reason == SessionSwitchReason.SessionUnlock)
{
//I returned to my desk
}
}
要了解他们登录或退出的时间,您只需检查当前机器上的用户对象UserPrincipal。
public DateTime UserLogon(string username)
{
// create your domain context
PrincipalContext domain = new PrincipalContext(ContextType.Domain);
// find the user
UserPrincipal foundUser = UserPrincipal.FindByIdentity(domain, IdentityType.Name, username);
//Return time for last logon
return foundUser.LastLogon;
}