C# 如何检测表单关闭的原因?

C# 如何检测表单关闭的原因?,c#,.net,winforms,C#,.net,Winforms,如果用户出于某种原因未关闭表单/应用程序(此处以及文中进一步提到的我已使用WinForms在C#中创建了自己的表单/应用程序),例如电脑突然关机或防病毒应用程序关闭我的应用程序,在这种情况下,当应用程序关闭时,我需要在同一个应用程序中创建一个文件,您在其中写入了应用程序关闭的原因。 我想在FormClosing事件处理程序或FormClosed中创建一个文件,以便在(FormClosingEventArgs)e.CloseReason中写入原因,但它不起作用。。。我决定通过KillProcess

如果用户出于某种原因未关闭表单/应用程序(此处以及文中进一步提到的我已使用WinForms在C#中创建了自己的表单/应用程序),例如电脑突然关机或防病毒应用程序关闭我的应用程序,在这种情况下,当应用程序关闭时,我需要在同一个应用程序中创建一个文件,您在其中写入了应用程序关闭的原因。 我想在FormClosing事件处理程序或FormClosed中创建一个文件,以便在(FormClosingEventArgs)e.CloseReason中写入原因,但它不起作用。。。我决定通过KillProcess程序测试它(我使用这个程序,因为,例如,如果我通过Windows任务管理器停止应用程序,那么文件将被写入用户自己停止了应用程序,因此我需要软件,以便我的应用程序不确定我关闭了它),从而关闭程序,但我的应用程序甚至没有时间闪烁,不是说它会创建一个文件并在那里写入有关终止原因的数据,一般来说,它不会创建或写入任何内容。 执行此任务的选项有哪些?我需要一个应用程序来完成这一切,也就是说,没有额外的应用程序来分析我的应用程序

使用处理程序编写代码:

        private void UserForm_FormClosing(object sender, FormClosingEventArgs e)
    {
        Console.WriteLine("Closing!!!");
        string path = @"C:\Users\Kulic\Desktop\reasonclose.txt";
        FileInfo fileInf = new FileInfo(path);

        switch (e.CloseReason)
        {
            case CloseReason.None:
                {
                    if (!File.Exists(path))
                    {
                        using (StreamWriter sw = File.CreateText(path))
                        {
                            sw.WriteLine("The reason for the closure was not determined or cannot be determined.");                                
                        }
                    }
                    break;
                }

            case CloseReason.WindowsShutDown:
                {
                    if (!File.Exists(path))
                    {
                        using (StreamWriter sw = File.CreateText(path))
                        {
                            sw.WriteLine("The operating system closes all applications before shutting down.");
                        }
                    }
                    break;
                }

            case CloseReason.MdiFormClosing:
                {
                    if (!File.Exists(path))
                    {
                        using (StreamWriter sw = File.CreateText(path))
                        {
                            sw.WriteLine("The parent form of this multi-document interface (MDI) form is closed.");
                        }
                    }
                    break;
                }

            case CloseReason.UserClosing:
                {
                    if (!File.Exists(path))
                    {
                        using (StreamWriter sw = File.CreateText(path))
                        {
                            sw.WriteLine("The form is closed programmatically or through user action in the UI (such as clicking the Close button in the window forms, select Close in the system menu of the window or by pressing ALT+F4).");
                        }
                    }
                    break;
                }

            case CloseReason.TaskManagerClosing:
                {
                    if (!File.Exists(path))
                    {
                        using (StreamWriter sw = File.CreateText(path))
                        {
                            sw.WriteLine("Microsoft Windows task Manager closes the application.");
                        }
                    }
                    break;
                }

            case CloseReason.FormOwnerClosing:
                {
                    if (!File.Exists(path))
                    {
                        using (StreamWriter sw = File.CreateText(path))
                        {
                            sw.WriteLine("The owner form is closed.");
                        }
                    }
                    break;
                }

            case CloseReason.ApplicationExitCall:
                {
                    if (!File.Exists(path))
                    {
                        using (StreamWriter sw = File.CreateText(path))
                        {
                            sw.WriteLine("The Exit() method of the Application class was called.");
                        }
                    }
                    break;
                }

            default:
                {
                    if (!File.Exists(path))
                    {
                        using (StreamWriter sw = File.CreateText(path))
                        {
                            sw.WriteLine("The reason for closing was not revealed");
                        }
                    }
                    break;
                }
        }
    }

FormClosing
事件只有在程序或窗体通过正常方式关闭时才有机会触发。使用任务管理器终止进程(或其他突然关闭进程的操作)只需跳过它和任何其他关闭代码

这不是代码中的错误,而是进程终止的本质:不管发生什么,立即停止。在这种情况下,甚至没有任何东西有机会运行,因为您的进程被终止。想象一下,当你拔掉电脑的插头,电脑就关机了,没有任何东西可以正常关机


没有办法抓住这样的事件。您可以得到正常的关闭原因,但不能得到异常的原因。

只有当程序或窗体通过正常方式关闭时,
FormClosing
事件才有机会触发。使用任务管理器终止进程(或其他突然关闭进程的操作)只需跳过它和任何其他关闭代码

这不是代码中的错误,而是进程终止的本质:不管发生什么,立即停止。在这种情况下,甚至没有任何东西有机会运行,因为您的进程被终止。想象一下,当你拔掉电脑的插头,电脑就关机了,没有任何东西可以正常关机


没有办法抓住这样的事件。您可以得到正常的关闭原因,但不能得到异常的关闭原因。

您也可以尝试订阅
AppDomain.ProcessExit
事件(),但请记住,即使这样也不能保证在所有情况下都会触发。这完全取决于你的程序是如何被终止的

例如,如果计算机意外断电或崩溃,则此后将不会运行任何操作,并且无法将关闭原因保存到文件中


此外,这超出了您的问题范围,但代码过于复杂。您只需检查
File.Exists
并打开流一次。您可以使用块将switch语句放入
中。

您也可以尝试订阅
AppDomain.ProcessExit
事件(),但请记住,即使这样也不能保证在所有情况下都会触发。这完全取决于你的程序是如何被终止的

例如,如果计算机意外断电或崩溃,则此后将不会运行任何操作,并且无法将关闭原因保存到文件中


此外,这超出了您的问题范围,但代码过于复杂。您只需检查
File.Exists
并打开流一次。您可以使用
块将switch语句放入
中。

我需要做一些其他事情:关闭我的应用程序后,我需要一个表单出现,用户可以在其中指定关闭应用程序原因的版本。根据你的回答,这是不可能的,对吗?@MaxWellViksna正确,当进程被终止、崩溃或以其他方式异常结束时。当程序正常关闭时,您的
FormClosing
将按您的意愿运行,您可以启动另一个表单或注册或其他任何操作。我需要做一些其他事情:关闭我的应用程序后,我需要一个表单出现,用户可以在其中指定其关闭应用程序原因的版本。根据你的回答,这是不可能的,对吗?@MaxWellViksna正确,当进程被终止、崩溃或以其他方式异常结束时。当程序正常关闭时,您的
FormClosing
将按您的意愿运行,您可以启动另一个表单或注册表或其他任何内容。