Shutdown computer

MiPakTeh 1,476 Reputation points
2021-10-17T09:07:25.397+00:00

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.
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,280 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Castorix31 81,746 Reputation points
    2021-10-17T11:40:27.137+00:00

    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

    0 comments No comments

  2. Jack J Jun 24,296 Reputation points Microsoft Vendor
    2021-10-18T08:30:07.703+00:00

    @MiPakTeh , 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. MiPakTeh 1,476 Reputation points
    2021-10-23T01:52:10.69+00:00

    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.