如何使用c#处理成功登录

本文关键字:成功 登录 处理 何使用 | 更新日期: 2023-09-27 18:11:04

我有c#应用程序,可以监控Logoff和SystemShutdown事件

class Program
{
    static void Main(string[] args)
    {
        SystemEvents.SessionEnding += SystemEvents_SessionEnding;
        Console.ReadLine();  //This is needed to keep the application running.
    }
    static void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
    {
        switch (e.Reason)
        {
            case SessionEndReasons.Logoff:
                MessageBox.Show("User logging off");
                break;
            case SessionEndReasons.SystemShutdown:
                MessageBox.Show("System is shutting down");
                break;
        }
    }
}

请帮助我,我如何才能监控(获取事件)用户登录事件?谢谢你

如何使用c#处理成功登录

假设您运行的是windows服务,您应该使用SessionSwitch处理程序的登录会话。

SystemEvents.SessionSwitch += SystemEvents_SessionSwitch;
        private void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
        {
            if(e.Reason==SessionSwitchReason.SessionLogon)
            {
            }
        }

也许你应该使用SessionSwitch事件:

static void Main(string[] args)
        {
            SystemEvents.SessionEnding += SystemEvents_SessionEnding;
            SystemEvents.SessionSwitch += SystemEvents_SessionSwitch;
            Console.ReadLine();  //This is needed to keep the application running.
        }
        static void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
        {
            switch (e.Reason)
            {
                case SessionSwitchReason.SessionLogon:
                    //do something
                    break;
            }
        }