我有一个Windows服务,可以获取用户详细信息并将结果保存到日志文本文件中.而且,我的问题是当我关闭或注销我的系统时,我也想将我的系统中的时间保存到该日志文件中.但是,我不知道该怎么做.
我检查了winproc方法来检测关机操作,但我无法在窗口服务上使用它,谷歌搜索发现它只能用于表单.我们如何检测用户是否已点击关机或注销并执行某些操作.所以,请给我一些想法或建议.
我已经将它用于注销,但是当我注销系统时,会在日志条目上进行注销
protected override void OnSessionChange(SessionChangeDescription changeDescription)
{
this.RequestAdditionalTime(250000); //gives a 25 second delay on Logoff
if (changeDescription.Reason == SessionChangeReason.SessionLogoff)
{
// Add your save code here
StreamWriter str = new StreamWriter("D:\\Log.txt", true);
str.WriteLine("Service stoped due to " + changeDescription.Reason.ToString() + "on" + DateTime.Now.ToString());
str.Close();
}
base.OnSessionChange(changeDescription);
}
Run Code Online (Sandbox Code Playgroud) 我创建了一个带有Timer的Windows服务,并且在触发事件中timer.Elapsed我正System.Diagnostics.Process.Start(exe path)以5秒的间隔创建一个process().但是这个过程不会在事件发生时创建.
有没有其他方法这样做?
提前致谢.
private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
Process pr = new Process();
pr.StartInfo.FileName = @"C:\Program Files\Messenger\msmsgs.exe";
pr.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
pr.StartInfo.CreateNoWindow = false;
pr.Start();
}
Run Code Online (Sandbox Code Playgroud)