获取Windows Server关闭原因在C# [英] Get Windows Server shutdown reason in C#

查看:161
本文介绍了获取Windows Server关闭原因在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户在对话窗口中选择原因后,是否可以立即在Windows Server 2008中得到关机原因?对于shutdown事件我正在使用SystemEvents.SessionEnding。
我想写Windows服务,这将发送关于这个事件的电子邮件。



或者有什么其他方式在Windows服务器发送电子邮件关于关机/重新启动事件,得到用户输入的原因?另外,我想通知有关电源变化(电线/电池),但是我已经通过Kernel32.dll> GetSystemPowerStatus解决了。

解决方案

您可以查看关闭原因来检查EventLog。



我在Windows窗体上组装了一个可以适应Windows服务的快速演示。 >

我已经将一个EventLog组件添加到窗体并正确配置。下面的代码片段显示了我通过设计师进行设置的InitializeComponent()中生成的代码。

  
this.eventLog1.EnableRaisingEvents = true;
this.eventLog1.Log =System;
this.eventLog1.Source =USER32;
this.eventLog1.SynchronizingObject = this;
this.eventLog1.EntryWritten + = new System.Diagnostics.EntryWrittenEventHandler(this.eventLog1_EntryWritten);

在事件处理程序中,您将具有以下几点:

  
private void eventLog1_EntryWritten(object sender,System.Diagnostics.EntryWrittenEventArgs e)
{
EventLogEntry entry = e。条目;
if(e.Entry.EventID == 1074)
{
File.AppendAllText(@c:\message.txt,entry.Message);
}
}

查看您的事件日志查看适当的EventIds以过滤掉。



编译器会警告您不要弃用EventID,并告诉您应该使用InstanceId,但是在快速测试中在这里完成,它没有写到我的日志文件,我想我们已经有足够的信息让你在轨道上。


Is it possible to get shutdown reason in Windows Server 2008 immediately after user choose the reason in dialog window? For the shutdown event I'm using SystemEvents.SessionEnding. I want to write windows service, which will send e-mail about this event.

Or is there any other way in windows server to send e-mails about shutdown/restart event with getting the reason entered by user? Also, I want to notify about power source change (electic line/battery), but this I have already solved by Kernel32.dll > GetSystemPowerStatus.

解决方案

You can get the shutdown reason inspecting the EventLog.

I assembled a quick demo on Windows Forms that you can adapt to your Windows service.

I've added a EventLog component to the Form and configured it properly. The snippet below shows the code generated in InitializeComponent() for the settings I've maid through the designer.


this.eventLog1.EnableRaisingEvents = true;
this.eventLog1.Log = "System";
this.eventLog1.Source = "USER32";
this.eventLog1.SynchronizingObject = this;
this.eventLog1.EntryWritten += new System.Diagnostics.EntryWrittenEventHandler(this.eventLog1_EntryWritten);

On the event handler, you'll have something along the following lines:


private void eventLog1_EntryWritten(object sender, System.Diagnostics.EntryWrittenEventArgs e)
{
    EventLogEntry entry = e.Entry;
    if (e.Entry.EventID == 1074)
    {
        File.AppendAllText(@"c:\message.txt", entry.Message);
    }
}

Take a look at your event log to see the appropriate EventIds to filter out.

The compiler will warn you about EventID being deprecated and telling you that you should use InstanceId, but in the quick tests I've done here, it didn't write to my log file and I think we already have enough information to put you on track.

这篇关于获取Windows Server关闭原因在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆