首页 文章

如何识别窗口服务中的登录事件

提问于
浏览
1

我有一个Windows服务,可以获取用户详细信息并将结果保存到日志文本文件中 . 而且,我的问题是当我注销我的系统并再次登录即没有重新启动机器..,我也想节省我将系统登录到该日志文件的时间..如何在窗口服务中编写登录事件..请帮助评论

我使用了以下代码,但登录时没有写入日志文本文件 . 即LogOn no-1或LogOn no-2 ...是否有任何错误或登录没有足够的时间来执行该过程..

Microsoft.Win32.SystemEvents.SessionSwitch += new Microsoft.Win32.SessionSwitchEventHandler(SystemEvents_SessionSwitch);
 void SystemEvents_SessionSwitch(object sender, Microsoft.Win32.SessionSwitchEventArgs e)
        {
            StreamWriter str = new StreamWriter("D:\\Log.txt", true);
            str.WriteLine("LogOn no-1: " + DateTime.Now.ToString());
            str.Close();
            if (e.Reason == SessionSwitchReason.SessionLogon)
            {
                StreamWriter str1 = new StreamWriter("D:\\Log.txt", true);
                str1.WriteLine("LogOn no-2: " + DateTime.Now.ToString());
                str1.Close();
            }
        }

3 回答

  • 1

    http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicebase.onsessionchange.aspx

    这可能是你最好的选择,因为Vista和Win7处理用户会话很像终端服务器 . 这应该让你处理会话更改,如果你想要会话ID或会话更改的原因(登录/注销/锁定等),它会给出一个带有相关信息的结构

  • 2

    看一下 SystemEvents 类,here's the MSDN link .

    在您的情况下相关的是暴露事件 SessionEndedSessionEndingSessionSwitch ,可能 PowerModeChanged .

    一个简单的例子可能如下所示:

    SystemEvents.SessionSwitch += OnSessionSwitch;
    
    void OnSessionSwitch(object sender, SessionSwitchEventArgs e)
    {
        //implement your logic here
    }
    
  • 0

    除非提供消息循环,否则不会引发这些事件;通过添加隐藏表单手动(或可能允许服务与桌面交互 - 不确定从不累,可能不建议) .

    我有一个类似的问题,其中一个服务没有收到TimeZone更改,所以不得不为服务添加一个隐藏的表单 .

    Here是如何解决问题的示例之一 .

    以下是我为解决问题所做的工作:

    将TimeZoneForm添加到service.sln;

    在Service的OnStart中添加以下代码: new System.Threading.Thread(RunMessagePump).Start();

    并将此方法添加到服务文件:

    private void RunMessagePump()
            {            
                Application.Run(new TimeZoneForm.TimeZoneForm());
            }
    
    
    internal class TimeZoneForm : Form
        {
            public TimeZoneForm()
            {
                InitializeComponent();
            }
    
            private void TimeZoneForm_Load(object sender, EventArgs e)
            {
                SystemEvents.TimeChanged += SystemEvents_TimeChanged;            
            }
    
            private void TimeZoneForm_Closing(object sender, FormClosingEventArgs e)
            {
                SystemEvents.TimeChanged -= SystemEvents_TimeChanged;            
            }
    
            private void SystemEvents_TimeChanged(object sender, EventArgs e)
            {
                System.Globalization.CultureInfo.CurrentCulture.ClearCachedData();
            }
    
            private System.ComponentModel.IContainer components = null;
    
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    
            private void InitializeComponent()
            {
                this.SuspendLayout();
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(0, 0);
                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
                this.Name = "TimeZoneForm";
                this.Text = "TimeZoneForm";
                this.WindowState = System.Windows.Forms.FormWindowState.Minimized;
                this.Load += this.TimeZoneForm_Load;
                this.FormClosing += this.TimeZoneForm_Closing;
                this.ResumeLayout(false);
    
            }
        }
    

相关问题