1

我在 C# 应用程序中工作。如何获取使用 c# 作为 Windows 服务登录系统的每个用户的登录和登录详细信息?现在我在系统关闭和启动时只获得登录和注销详细信息。

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.IO;
using System.Timers;
using System.Management;
using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;
using System.Web;
using System.Runtime.InteropServices;
using System.Windows.Forms;

//using System.Configuration;
namespace WinServiceAcc
{
    public partial class WinServiceAcc : ServiceBase
    {`
        [DllImport("user32.dll")]
        public static extern int ExitWindowsEx(int uFlags, int dwReason);
        System.Timers.Timer timer = new System.Timers.Timer();
        public String UserName { get; set; }
        public String Password { get; set; }

        public WinServiceAcc()
        {
            InitializeComponent();
        }
        protected override void OnStart(string[] args)
        {
            try
            {
                //DirectoryEntry dir = new DirectoryEntry("LDAP://production");
                //base.OnStart(args);
                //UserName = args[0];
                //Password = args[1];  
                Startup();
                // Password = (string)collection.Cast<ManagementBaseObject>().First()["Password"];            }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadLine();
            }            
        }


        private void Startup()
        {



            timer.Enabled = true;
            timer.Interval = 60000;          
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT UserName FROM Win32_ComputerSystem");
            ManagementObjectCollection collection = searcher.Get();
            UserName = (string)collection.Cast<ManagementBaseObject>().First()["UserName"];
            timer.Elapsed += new ElapsedEventHandler(onElapsedTime);
            TraceService("Service started");
        }
        protected override void OnStop()
        {
           timer.Enabled = false;
           TraceService("Service stoped");
        }
       protected override void OnSessionChange(SessionChangeDescription changeDescription)
        {
            //TraceService("Logout Uname: " + UserName + " on " + DateTime.Now.ToString("dd/MM/yyyy") + " at " + DateTime.Now.ToString("hh:mm tt"));
            //timer.Enabled = false; 
        }
        protected override void OnPause()
        {
            ExitWindowsEx(0, 0);
            Application.SetSuspendState(PowerState.Hibernate, true, true);
            TraceService("Logout Uname: " + UserName + " on " + DateTime.Now.ToString("dd/MM/yyyy") + " at " + DateTime.Now.ToString("hh:mm tt"));
           timer.Enabled = false; 
        }      
        protected override void OnContinue()
        {
            ExitWindowsEx(2, 0);
            TraceService("Login Uname: " + UserName + " on " + DateTime.Now.ToString("dd/MM/yyyy") + " at " + DateTime.Now.ToString("hh:mm tt"));
            timer.Enabled = false; 
        }
      protected override void OnShutdown()
        {
            ExitWindowsEx(1, 0);
           TraceService("Logout Uname: " + UserName + " on " + DateTime.Now.ToString("dd/MM/yyyy") + " at " + DateTime.Now.ToString("hh:mm tt"));
           timer.Enabled = false;  
      }
        private void onElapsedTime(object sender, ElapsedEventArgs e)
        {
            TraceService("Login as Uname: " + UserName + " on " + DateTime.Now.ToString("dd/MM/yyyy") + " at " + DateTime.Now.ToString("hh:mm tt"));
            timer.Enabled = false;
        }
        private void TraceService(string content)
        {
            FileStream fs = new FileStream(@"c:\ServicesList.txt", FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs);
            sw.BaseStream.Seek(0, SeekOrigin.End);
            sw.WriteLine(content);
            sw.Flush();
            sw.Close();
        }
    }
}
4

0 回答 0