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
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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.
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
@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.
Hi jackjjun, thank for your feedback.
I follow your method but still have message before restart computer.
I try type something in Notepad then restart computer.