小编ham*_*iri的帖子

C#取消Windows关机

我想我的应用程序可以防止Windows关闭.我知道有一个系统命令来做到这一点.但不适合我的计划.我使用此代码"取消"窗口关闭:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    if (e.CloseReason.Equals(CloseReason.WindowsShutDown))
    {
        MessageBox.Show("Cancelling Windows shutdown");
        string cmd = "shutdown /a";
        Process.Start(cmd);// for executing system command.
    }
}
Run Code Online (Sandbox Code Playgroud)

并使用此代码,但不起作用:(:

public Form1()
{
    InitializeComponent();

    SystemEvents.SessionEnding += SessionEndingEvtHandler;
}

private void SessionEndingEvtHandler(object sender, SessionEndingEventArgs e)
{
    MessageBox.Show("Cancelling Windows shutdown");
    string cmd = "shutdown /a";
    Process.Start(cmd);// for executing system command.  
}
Run Code Online (Sandbox Code Playgroud)

如果有人解释我怎么能在"取消"关闭窗口,我将不胜感激.谢谢

c# windows command-line system event-handling

4
推荐指数
1
解决办法
8151
查看次数

标签 统计

c# ×1

command-line ×1

event-handling ×1

system ×1

windows ×1