在 C# 中获取 Windows Server 关闭原因

发布于 2024-08-02 19:20:03 字数 253 浏览 9 评论 0原文

用户在对话框窗口中选择原因后,是否可以立即在 Windows Server 2008 中获取关机原因?对于关闭事件,我使用 SystemEvents.SessionEnding。 我想编写 Windows 服务,它将发送有关此事件的电子邮件。

或者Windows服务器中有没有其他方法可以发送有关关闭/重新启动事件的电子邮件并获取用户输入的原因?另外,我想通知有关电源更改(电线/电池)的信息,但我已经通过 Kernel32.dll > 解决了这个问题获取系统电源状态。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜警司 2024-08-09 19:20:03

您可以通过检查事件日志来获取关闭原因。

我在 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.Entry;
    if (e.Entry.EventID == 1074)
    {
        File.AppendAllText(@"c:\message.txt", entry.Message);
    }
}

查看事件日志以查看要过滤掉的相应 EventId。

编译器会警告您 EventID 已被弃用,并告诉您应该使用 InstanceId,但在我在这里完成的快速测试中,它没有写入我的日志文件,我认为我们已经有足够的信息来告诉您追踪。

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.

跨年 2024-08-09 19:20:03

当然有可能。
如果您想实时获取该组合框值,则需要在该进程上运行线程监视器以​​在该值更改时引发事件。

sure it's possible.
in case you want to get that comboBox value in real-time, you will need to run a Thread monitor on that process to raise an event when that value change.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文