且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如果我的应用程序正在运行,如何在Windows关闭时发出警告

更新时间:2023-10-20 16:05:58

参见Form.FormClosing事件 [ ^ ]

检查FormClosingEventArgs的CloseReason属性,如果是CloseReason.WindowsShutDown然后将e.Cancel设置为True则应该阻止表单立即关闭。

然后,你可以使用
See the Form.FormClosing event[^]
Check the CloseReason property of the FormClosingEventArgs, if it is CloseReason.WindowsShutDown then set e.Cancel to True then it should prevent the form closing immediately.
Then, you can stop shutdown by using
Process.Start("shutdown", "-a")

我没有在Win8或Win10中试过这个,但是它应该仍然有用。

I haven't tried this in Win8 or Win10, but it should still work.