2012-01-30 41 views
12

可能重复:
How can I programmatically determine if my workstation is locked?
Checking for workstation lock/unlock change with c#确定当用户锁定/解锁其PC

我想确定时,在Windows(XP,或7)机被锁定,并解锁。

我想创建一个时间表活动记录应用程序,预填充时间,午餐,休息等等,将是非常有用的。

当Windows操作系统被锁定和解锁时,是否可以启动一个事件?

+3

我以前做过这件事,但这听起来很邪恶。 – Jodrell 2012-01-30 17:22:29

+2

检查这一个:http://www.codeproject.com/Articles/13774/Detecting-Windows-Workstation-Locked-Unlocked-in-N – 2012-01-30 17:23:03

+6

你介意让我知道你在哪里工作,所以我可以将它添加到我的黑名单。 – 2012-01-30 17:23:28

回答

0

你需要获得一个会话变更通知,通过看on PInvoke here

正如注释状态开始,有人应该考虑这种使用这个API的对商业的影响。

一旦用户意识到这种监控,他们可能会试图离开会话解锁,甚至共享密码,这将是反生产力。

+1

这是一个应用程序让我用来帮助提醒我,当我在,离开我的办公桌等,因为当我填写时间表,这不是'公司' – cometbill 2012-02-27 11:03:21

14

试试这个(found here):

SystemEvents.SessionSwitch += 
    new SessionSwitchEventHandler(SystemEvents_SessionSwitch); 

// unhook static eventhandler when application terminates! 

//Handle event 
static void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e) 
{ 
    Console.WriteLine(e.Reason.ToString()); 
} 
+0

这实际上是一种更简单的方法(和我之前做过的)+1 – Jodrell 2012-01-30 17:33:58

+0

*(对不起,编辑错误的答案)* – RedFilter 2012-01-30 17:38:07

+2

您真的需要在应用程序终止时解除事件处理程序吗?它不会自动发生吗?你怎么做? – mcmillab 2016-12-28 22:20:57

8

这里是一个工作示例。运行此操作,锁定并解锁工作站,并观察输出:

using System; 
using Microsoft.Win32; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     private static SessionSwitchEventHandler sseh; 

     static void Main(string[] args) 
     { 
      sseh = new SessionSwitchEventHandler(SystemEvents_SessionSwitch); 
      SystemEvents.SessionSwitch += sseh; 
      while(true) {} 
     } 

     static void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e) 
     { 
      Console.WriteLine(e.Reason.ToString()); 
     } 

    } 
} 

您可能还想检测屏幕保护程序何时运行。