C#如何监测电脑将要重启?C# 电脑重启

解决方案 »

  1.   

    Microsoft.Win32.SystemEvents.SessionEnding += ...
      

  2.   

    谢谢了 找到方法了
    这是MSDN给的实例代码:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
            private static int WM_QUEYENDSESSION = 0x11;
            private static bool systemShutdown = false;        protected override void WndProc(ref System.Windows.Forms.Message m)
            {
                if (m.Msg==WM_QUEYENDSESSION)
                {
                    MessageBox.Show("queryendsession:这是一个注销或重新启动,关机,");
                    systemShutdown = true;
                }
                base.WndProc(ref m);
            }        public Form1()
            {
                InitializeComponent();
            }        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                if (systemShutdown)
                {
                    systemShutdown = false;
                    if (DialogResult.Yes == MessageBox.Show("系统信息", "你想登出前保存你的工作?",
                        MessageBoxButtons.YesNo))
                    {
                        e.Cancel = true;
                    }
                    else
                    {
                        e.Cancel = false;
                    }
                }
            }
        }
    }