开发者

Prevent windows Shutdown with CANCEL option C#

开发者 https://www.devze.com 2023-03-15 09:22 出处:网络
I\'m trying to include in my Windows Form App a way that when user tries to shutdown windows it opens a dialog box with CANCEL option....that office classic one.

I'm trying to include in my Windows Form App a way that when user tries to shutdown windows it opens a dialog box with CANCEL option....that office classic one.

In other topics, people describes how to 开发者_如何转开发prevent windows shutdown. They use a dialog box for this. It helps but if the user immediately clicks in any option in this box, windows closes the application.

You can understand what I'm meaning, doing the following test:

In windows Vista or 7 Open Paint, Word or any office soft and begin writing something. Do not save it.

Try to shutdown windows and when the classic save dialog box appears IMMEDIATELY click in "cancel".

You will see that the application continues to work and windows is asking you what you want to do.

I tried to follow this Microsoft Link but if I click "ok" in Message Box, the App closes.


   private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (e.CloseReason.Equals(CloseReason.WindowsShutDown))
        {
           if (MessageBox.Show("You are closing this app.\n\nAre you sure you wish to exit ?", "Warning: Not Submitted", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Stop) == DialogResult.Yes)
               return;    
           else    
               e.Cancel = true;
        }
    }


Although delay windows shutdown is not recommended it is possible using shutdown scripts. You can configure your own script file using gpedit.msc configuration file. I have read that these scripts can delay the windows shutdown up to 10 minutes.

I hope it helps

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号