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:
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 :( :
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
This is strongly ill advised and Microsoft make it as hard as possible to do this. If a user wants to shut down, then it is the user's responsiblity, not the applications. As per the Microsoft article Application Shutdown Changes in Windows Vista:
...also...
If you really do need to intevene during shutdown, there is a new API which you should register with:
But at the end of the day, all this will do is present a user interface to the user to say that application is preventing shutdown and asking the user if they want to continue and force shutdown anyway. If they answer yes, you can't block this, and there is no way to block the UI.
Read the MSDN article I've linked to - it explians the model from Vista onwards. Ultimately, the paradigm is one of giving users control, and preventing applications overriding user demands.