回复 10楼 athenalux
我很费解啊 ,真的不行么我都用它定时关机或者按要求关机了。
难不成我的 XP 牛X些???
我还单独是了代码,秒关。

QQ:491697374 Mail:vxbb@
程序代码:using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
namespace ShutDownComputer
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
///
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
= new System.Windows.();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.timer1 = new System.Timers.Timer();
(()(this.timer1)).BeginInit();
this.SuspendLayout();
//
// button1
//
this.button1.BackColor = System.Drawing.Color.Olive;
this.button1.Location = new System.Drawing.Point(32, 80);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "开始";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 40);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(120, 23);
this.label1.TabIndex = 2;
this.label1.Text = "您想让你的计算机在";
//
// comboBox1
//
(new object[] {
"10",
"20",
"30",
"40",
"50",
"60",
"70",
"80",
"90",
"100",
"110",
"120"});
= new System.Drawing.Point(16, 56);
= "comboBox1";
= new System.Drawing.Size(40, 20);
= 3;
= "10";
//
// label2
//
this.label2.Location = new System.Drawing.Point(56, 56);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(96, 16);
this.label2.TabIndex = 4;
this.label2.Text = "分之后自动关机";
//
// label3
//
this.label3.Location = new System.Drawing.Point(24, 8);
this.label3.Name = "label3";
this.label3.TabIndex = 5;
//
// timer1
//
this.timer1.Interval = 1000;
this.timer1.SynchronizingObject = this;
this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Elapsed);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(192)), ((System.Byte)(255)));
this.ClientSize = new System.Drawing.Size(152, 109);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add();
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
(()(this.timer1)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
[StructLayout(LayoutKind.Sequential, Pack=1)]
internal struct TokPriv1Luid
{
public int Count;
public long Luid;
public int Attr;
}
[DllImport("kernel32.dll", ExactSpelling=true) ]
internal static extern IntPtr GetCurrentProcess();
[DllImport("advapi32.dll", ExactSpelling=true, SetLastError=true) ]
internal static extern bool OpenProcessToken( IntPtr h, int acc, ref IntPtr phtok );
[DllImport("advapi32.dll", SetLastError=true) ]
internal static extern bool LookupPrivilegeValue( string host, string name, ref long pluid );
[DllImport("advapi32.dll", ExactSpelling=true, SetLastError=true) ]
internal static extern bool AdjustTokenPrivileges( IntPtr htok, bool disall,
ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen );
[DllImport("user32.dll", ExactSpelling=true, SetLastError=true) ]
internal static extern bool ExitWindowsEx( int flg, int rea );
internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
internal const int TOKEN_QUERY = 0x00000008;
internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
internal const int EWX_LOGOFF = 0x00000000;
internal const int EWX_SHUTDOWN = 0x00000001;
internal const int EWX_REBOOT = 0x00000002;
internal const int EWX_FORCE = 0x00000004;
internal const int EWX_POWEROFF = 0x00000008;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.Windows. comboBox1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Timers.Timer timer1;
internal const int EWX_FORCEIFHUNG = 0x00000010;
private void DoExitWin( int flg )
{
bool ok;
TokPriv1Luid tp;
IntPtr hproc = GetCurrentProcess();
IntPtr htok = IntPtr.Zero;
ok = OpenProcessToken( hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok );
tp.Count = 1;
tp.Luid = 0;
tp.Attr = SE_PRIVILEGE_ENABLED;
ok = LookupPrivilegeValue( null, SE_SHUTDOWN_NAME, ref tp.Luid );
ok = AdjustTokenPrivileges( htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero );
ok = ExitWindowsEx( flg, 0 );
}
DateTime ShutDownItTime = new DateTime();
int status = 0; //0: 表示还没有开始计时关机; 1: 已经开始计时关机
private void button1_Click(object sender, System.EventArgs e)
{
if(status == 1)
{
this.timer1.Enabled = false;
status = 0;
this.button1.Text = "开始";
return;
}
int intTime = 0;
if( == 0)
{
this.label3.Text = "请输入时间!";
return;
}
try
{
intTime = int.Parse();
}
catch
{
this.label3.Text = "时间只能为整数,请重新输入!";
}
this.ShutDownItTime=DateTime.Now.AddMinutes(intTime);
this.timer1.Enabled = true;
status = 1;
this.button1.Text = "停止";
}
private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if(DateTime.Now >= this.ShutDownItTime)
{
this.label3.Text = "正在关机...";
DoExitWin(EWX_SHUTDOWN);
}
}
}
}

程序代码:using System;
using System.Collections.Generic;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace my
{
public partial class shutdown : UserControl
{
public shutdown()
{
InitializeComponent();
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
private struct tokpriv1luid
{
public int count;
public long luid;
public int attr;
}
[DllImport("kernel32.dll", ExactSpelling = true)]
private static extern IntPtr GetCurrentProcess();
[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
private static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);
[DllImport("advapi32.dll", SetLastError = true)]
private static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);
[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
private static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall, ref tokpriv1luid newst, int len, IntPtr prev, IntPtr relen);
[DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
private static extern bool ExitWindowsEx(int doflag, int rea);
private const int se_privilege_enabled = 0x00000002;
private const int token_query = 0x00000008;
private const int token_adjust_privileges = 0x00000020;
private const string se_shutdown_name = "seshutdownprivilege";
private const int ewx_logoff = 0x00000000;
private const int ewx_shutdown = 0x00000001;
private const int ewx_reboot = 0x00000002;
private const int ewx_force = 0x00000004;
private const int ewx_poweroff = 0x00000008;
internal const int ewx_forcehung = 0x00000010;
private static void doexitwin(int doflag)
{
tokpriv1luid tp;
IntPtr hproc = GetCurrentProcess(); //句柄,操作系统指向每个窗体的对象 获取当前运行窗体的句柄
IntPtr htok = IntPtr.Zero; //空句柄
OpenProcessToken(hproc, token_adjust_privileges | token_query, ref htok); //打开当前应用程序的伪句柄
tp.count = 1;
tp.luid = 0;
tp.attr = se_privilege_enabled; //授权
LookupPrivilegeValue(null, se_shutdown_name, ref tp.luid); //检查是否可以授权,必要
AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);//授权
ExitWindowsEx(doflag, 0); //关机函数
}
private void button1_Click(object sender, EventArgs e)
{
doexitwin(ewx_force | ewx_reboot);
}
private void button2_Click(object sender, EventArgs e)
{
doexitwin(ewx_force | ewx_logoff);
}
private void button3_Click(object sender, EventArgs e)
{
doexitwin(ewx_force | ewx_poweroff);
}
string taskType="重启";
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
if (radioButton1.Checked)
{
taskType = "重启";
}
else if (radioButton2.Checked)
{
taskType = "注销";
}
else
{
taskType = "关机";
}
}
private void button4_Click(object sender, EventArgs e)
{
if (DateTime.Now.Hour > 9)
{
this.textBox1.Text = DateTime.Now.Hour.ToString();
}
else
{
this.textBox1.Text = "0" + DateTime.Now.Hour.ToString();
}
if (DateTime.Now.Minute > 9)
{
this.textBox2.Text = DateTime.Now.Minute.ToString();
}
else
{
this.textBox2.Text = "0" + DateTime.Now.Minute.ToString();
}
if (DateTime.Now.Second > 9)
{
this.textBox3.Text = DateTime.Now.Second.ToString();
}
else
{
this.textBox3.Text = "0" + DateTime.Now.Second.ToString();
}
}
private void textBox1_Leave(object sender, EventArgs e)
{
string input = textBox1.Text;
string Input = input == "" ? "00" : input;
try
{
if (int.Parse(Input) >= 0 && int.Parse(Input) <= 24)
{
if (int.Parse(Input) <= 9)
{
textBox1.Text = "0" + Input;
}
}
else
{
MessageBox.Show("你输入的时间有误!");
textBox1.Text = "00";
}
}
catch
{
MessageBox.Show("你输入的时间有误!");
textBox1.Text = "00";
}
}
private void textBox2_Leave(object sender, EventArgs e)
{
string input = textBox2.Text;
string Input = input == "" ? "00" : input;
try
{
if (int.Parse(Input) >= 0 && int.Parse(Input) <= 60)
{
if (int.Parse(Input) <= 9)
{
textBox2.Text = "0" + Input;
}
}
else
{
MessageBox.Show("你输入的时间有误!");
textBox2.Text = "00";
}
}
catch
{
MessageBox.Show("你输入的时间有误!");
textBox2.Text = "00";
}
}
private void textBox3_Leave(object sender, EventArgs e)
{
string input = textBox3.Text;
string Input = input == "" ? "00" : input;
try
{
if (int.Parse(Input) >= 0 && int.Parse(Input) <= 60)
{
if (int.Parse(Input) <= 9)
{
textBox3.Text = "0" + Input;
}
}
else
{
MessageBox.Show("你输入的时间有误!");
textBox3.Text = "00";
}
}
catch
{
MessageBox.Show("你输入的时间有误!");
textBox3.Text = "00";
}
}
DateTime dtExecutTime;
private void button5_Click(object sender, EventArgs e)
{
if (this.button5.Text == "启动任务")
{
dtExecutTime = new DateTime(this.dateTimePicker1.Value.Year, this.dateTimePicker1.Value.Month, this.dateTimePicker1.Value.Day, int.Parse(textBox1.Text), int.Parse(textBox2.Text), int.Parse(textBox3.Text));
if (dtExecutTime < DateTime.Now)
{
MessageBox.Show("你选择的时间无效");
return;
}
timer1.Start();
button5.Text = "取消任务";
this.dateTimePicker1.Enabled = false;
this.textBox1.Enabled = false;
this.textBox2.Enabled = false;
this.textBox3.Enabled = false;
this.button4.Enabled = false;
}
else
{
timer1.Stop();
button5.Text = "启动任务";
label6.Text = "";
}
}
private void timer1_Tick(object sender, EventArgs e)
{
DateTime dtnow = DateTime.Now;
TimeSpan ts = dtExecutTime - dtnow;
label6.Text = string.Format("距离你设置的任务----{0},还有{1}日{2}小时{3}分{4}秒!", taskType, ts.Days, ts.Hours, ts.Minutes, ts.Seconds);
if (dtnow.ToString() == dtExecutTime.ToString())
{
if (taskType == "注销")
{
timer1.Enabled = false;
doexitwin(ewx_force | ewx_logoff);
}
else if (taskType == "重启")
{
timer1.Enabled = false;
doexitwin(ewx_force | ewx_reboot);
}
else
{
timer1.Enabled = false;
doexitwin(ewx_force | ewx_poweroff);
}
}
}
}
}
