有很多的软件都实现了自动关机这样的功能。前一阵在Codeproject上看到一个这样的程序,整理了一下共享给大家。里面实现了关机,重启,注销等功能.
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
namespace TimerComputerShutdown
{
/// <summary>
/// Summary description for TimerComputerShutdown.
/// </summary>
public class TimerComputerShutdown : System.Windows.Forms.Form
{
private System.Windows.Forms.Label labelWhatDoYouWantTheComputerToDo;
private System.Windows.Forms.ComboBox comboBox;
private System.Windows.Forms.Label labelSelectDateTimeToShutdownComputer;
private System.Windows.Forms.DateTimePicker dateTimePicker;
private System.Windows.Forms.Label labelDateTimeNow;
private System.Windows.Forms.Label labelDateTimeShutdown;
private System.Windows.Forms.Label labelNow;
private System.Windows.Forms.Label labelShutdown;
private System.Windows.Forms.Panel panelParent;
private System.Windows.Forms.Panel panelChild;
private System.Windows.Forms.CheckBox checkBox;
private System.Windows.Forms.Button buttonCancel;
private System.Windows.Forms.Button buttonInvoke;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private System.Threading.ThreadStart threadStart;
private System.Threading.Thread thread;
[DllImport("user32.dll")] private static extern
bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")] private static extern
bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")] private static extern
bool IsIconic(IntPtr hWnd);
private const int SW_HIDE = 0;
private const int SW_SHOWNORMAL = 1;
private const int SW_SHOWMINIMIZED = 2;
private const int SW_SHOWMAXIMIZED = 3;
private const int SW_SHOWNOACTIVATE = 4;
private const int SW_RESTORE = 9;
private const int SW_SHOWDEFAULT = 10;
public TimerComputerShutdown()
{
InitializeComponent();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(TimerComputerShutdown));
this.labelWhatDoYouWantTheComputerToDo = new System.Windows.Forms.Label();
this.comboBox = new System.Windows.Forms.ComboBox();
this.labelSelectDateTimeToShutdownComputer = new System.Windows.Forms.Label();
this.dateTimePicker = new System.Windows.Forms.DateTimePicker();
this.labelDateTimeNow = new System.Windows.Forms.Label();
this.labelDateTimeShutdown = new System.Windows.Forms.Label();
this.labelNow = new System.Windows.Forms.Label();
this.labelShutdown = new System.Windows.Forms.Label();
this.panelParent = new System.Windows.Forms.Panel();
this.panelChild = new System.Windows.Forms.Panel();
this.buttonCancel = new System.Windows.Forms.Button();
this.buttonInvoke = new System.Windows.Forms.Button();
this.checkBox = new System.Windows.Forms.CheckBox();
this.panelParent.SuspendLayout(