C#如何检测kill事件发送方

C#如何检测kill事件发送方,c#,detect,shutdown,C#,Detect,Shutdown,我有一个FormClosing方法,让我的程序在关闭表单之前询问用户是否要退出程序。这很好,但我不想在程序因系统关闭/注销而关闭时显示对话框。如何检测kill命令是由系统发送的,而不是由用户单击表单的x发送的环境。hashutdownstarted不起作用 private void Form1_FormClosing(object sender, FormClosingEventArgs e) { // Need to check for system shutdown here bef

我有一个
FormClosing
方法,让我的程序在关闭表单之前询问用户是否要退出程序。这很好,但我不想在程序因系统关闭/注销而关闭时显示对话框。如何检测kill命令是由系统发送的,而不是由用户单击表单的x发送的<代码>环境。hashutdownstarted不起作用

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    // Need to check for system shutdown here before the next if is activated
    if (MessageBox.Show(...) == DialogResult.No)
    {
        e.Cancel = true;
        this.Activate();
    }
}

尝试检查
e.CloseReason
,例如

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    if (e.CloseReason != CloseReason.WindowsShutDown)
    {
        if (MessageBox.Show(...) == DialogResult.No)
        {
            e.Cancel = true;
            this.Activate();
        }
    }   
}
见: