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.