question

MiPakTeh-6272 avatar image
0 Votes"
MiPakTeh-6272 asked JackJJun-MSFT commented

Shutdown computer

Hi All,
What try to do;

If I want shutdown the computer at the time I need, then skip all the windows ask about saving file, What code to add bellow.
How?.
somebody can show me.

 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows.Forms;
 using System.Diagnostics;
 using System.Management;
    
 namespace Shutdown_
 {
    
     public partial class Form1 : Form
     {
         private System.Windows.Forms.Timer timer1;
         private int counter = 10;
    
    
         public Form1()
         {
             InitializeComponent();
    
             timer1 = new System.Windows.Forms.Timer();
    
             timer1 = new System.Windows.Forms.Timer();
             timer1.Tick += new EventHandler(timer1_Tick);
             timer1.Interval = 1000; // 1 second
             timer1.Start();
             textBox1.Text = counter.ToString();
         }
         private void timer1_Tick(object sender, EventArgs e)
         {
             counter--;
             textBox1.Text = counter.ToString();
    
             if (counter == 0)
             {
                 timer1.Stop();
    
                 ManagementBaseObject mboShutdown = null;
                 ManagementClass mcWin32 = new ManagementClass("Win32_OperatingSystem");
                 mcWin32.Get();
    
                 mcWin32.Scope.Options.EnablePrivileges = true;
                 ManagementBaseObject mboShutdownParams =
                          mcWin32.GetMethodParameters("Win32Shutdown");
    
                 mboShutdownParams["Flags"] = "1";
                 mboShutdownParams["Reserved"] = "0";
                 foreach (ManagementObject manObj in mcWin32.GetInstances())
                 {
                     mboShutdown = manObj.InvokeMethod("Win32Shutdown", mboShutdownParams, null);
                 }
    
             }
    
             else if (counter > 0)
             {
                 this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Some);
                 this.KeyPreview = true;
             }
         }
         private void Some(object sender, System.Windows.Forms.KeyEventArgs e)
         {
             if (e.KeyCode == Keys.F2 && e.Modifiers == Keys.Alt)
             {
                 System.Environment.Exit(0);
             }
         }
    
    
    
     }
 }
    
    
 thank.
dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Castorix31 avatar image
0 Votes"
Castorix31 answered

If I want shutdown the computer at the time I need, then skip all the windows ask about saving file, What code to add bellow.

If you want to force the Shutdown, use the EWX_FORCE flag with ExitWindowsEx
like in this thread


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

JackJJun-MSFT avatar image
0 Votes"
JackJJun-MSFT answered Castorix31 commented

@MiPakTeh-6272, based on my research, It is hard to use to c# code to skip all the windows that mention saving file.

However, I find a workaround to set Registry to shut down the computer directly.

First, Please Open Registry and Go to HKEY_USERS.DEFAULT\Control Panel\Desktop.

Second, Please click Edit > New > String Value.

Third, Please Give it the name AutoEndTasks and set Value data to 1 and click OK.

Finally, Please restart your computer to save the effect.

Based on my test, after I execute the above steps, I can use the code to shut down the computer directly.


If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.






· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

based on my research, It is hard to use to c# code to skip all the windows that mention saving file.

With EWX_FORCE, the PC is rebooted immediately (tested on Windows 10 1909)

0 Votes 0 ·

@Castorix31, yes, I know. But the customer wants to shut down the computer instead of reboot.

0 Votes 0 ·

ExitWindowsEx shuts down the computer...

0 Votes 0 ·
MiPakTeh-6272 avatar image
0 Votes"
MiPakTeh-6272 answered JackJJun-MSFT commented

Hi jackjjun, thank for your feedback.

I follow your method but still have message before restart computer.
143078-capture.gif



I try type something in Notepad then restart computer.


capture.gif (47.9 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@MiPakTeh-6272, as usual, we need to restart the computer to adapt the change of Registry.

0 Votes 0 ·