簡體   English   中英

如何識別窗口服務中的登錄事件

[英]how to identify logon event in window service

我有一個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();
            }
        }

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

這可能是最好的選擇,因為Vista和Win7就像終端服務器一樣處理用戶會話。 如果您需要會話ID或會話更改的原因(登錄/注銷/鎖定等),這應該可以讓您處理會話更改,並提供具有相關信息的結構。

看一下SystemEvents類, 這是MSDN鏈接

與您的情況相關的是暴露的事件SessionEndedSessionEndingSessionSwitch以及潛在的PowerModeChanged

一個簡單的示例可能如下所示:

SystemEvents.SessionSwitch += OnSessionSwitch;

void OnSessionSwitch(object sender, SessionSwitchEventArgs e)
{
    //implement your logic here
}

除非提供消息循環,否則不會引發這些事件。 通過添加隱藏的表單手動添加(或可能允許服務與桌面交互-不確定永遠不會累,可能不建議這樣做)。

我遇到一個類似的問題,其中一項服務未收到TimeZone更改,因此必須向該服務添加隱藏表單。

是有關如何解決此問題的示例之一。

以下是我為解決問題所做的工作:

將TimeZoneForm添加到service.sln;

然后在服務的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);

        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM