0

I want to my application can prevent from windows shutting down. I know that there is a system command to do that. But don't work for my program.

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.
    }
}
  • Try with shutdown -a – Mate Mar 04 '23 at 14:25
  • The days that this was still possible are over and done with, Vista put an end to it. Won't be missed. – Hans Passant Mar 04 '23 at 14:28
  • This may need to be run as an administrative user. Add an `Application Manifest File` to your project and modify it so that your project requires administrator privileges to run. Also, that's not the correct usage of [System.Diagnostics.Process](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process?view=net-7.0). The `Filename` should be the fully-qualified path. Then for `Arguments` you'll pass `/a`. See [ProcessStartInfo](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.processstartinfo?view=net-7.0). You may (or may not) need to set `UseShellExecute = true;` – Tu deschizi eu inchid Mar 04 '23 at 15:34
  • The following may be helpful: https://stackoverflow.com/a/71344930/10024425 – Tu deschizi eu inchid Mar 04 '23 at 15:37
  • Do you want to prevent automatic shutdown while the user is inactive or manual shutdown initiated by the user? – NineBerry Mar 04 '23 at 18:01
  • @DanielA.White the duplicate link is incorrect, that refers to *preventing* a shutdown from being triggered, not cancelling it once started. – Charlieface Mar 04 '23 at 22:43

0 Answers0