Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello all.
i want to my application can prevents from windows shut down. i know that there is a system command to do that. but don't work for my program.
i use this code for "cancel" the windows shut down:

C#
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.
           }
       }


and also use this code, but does not work :( :
C#
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.
            
            
        }


i would be grateful if anyone explain me how can in "cancel" windows shutdown.
thanks
Posted

It's much simpler. Use your SessionEndingEvtHandler function and do:
e.Cancel = true;
That's it already.
 
Share this answer
 
Comments
Sushil Mate 18-Jul-13 8:24am    
:) I didn't read it properly. my bad.
Jean A Brandelero 18-Jul-13 16:15pm    
Nice and clear. Helped me too, thanks.
C#
public Form1()
        {
            InitializeComponent();
 
            SystemEvents.SessionEnding += SessionEndingEvtHandler;
               
            
        }
 private void SessionEndingEvtHandler(object sender, SessionEndingEventArgs e)
        {
           e.Cancel = true;
            
            
        }



For more info: MSDN[^]

http://stackoverflow.com/a/13894368[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900