想做个定时关机重启的窗体程序 对关机重启的实现 网上找了下 说:使用C#调用cmd.exe执行shutdown.exe命令就行。可行吗?可以的话 是怎么调用的? 还有其它简单的方法吗 麻烦进贴的各位了
解决方案 »
- 爱心加油站
- c# 中 spread 怎样使用啊?
- 怎么判断一个一个server支部支持.net呀
- 请教各位C#熟手,在C#中怎样去除字符串前后空格??
- 开发框架开个源,(含并行计算及中英文分词功能整并整合EXTJS,各类AJAX控件数百)
- 批处理改名字
- 讨论:struct 和 class的异同。
- 请问重装系统之后vs.net还能用么?
- 声明一下,我没有倒分,只是刚刚得到一个OA的源代码,呵呵,所以给分了,网管不让我给,我才这样的,我现在还少一个短消息的功能模块,谁有给1500
- 请问各位大侠:在C#中如何封送并调用API函数GetClassInfo?
- c#中的suo文件是什么文件
- 求C#Winform操作数据库的MVC模式的样例代码
用法: shutdown [/i | /l | /s | /r | /g | /a | /p | /h | /e] [/f]
[/m \\computer][/t xxx][/d [p|u:]xx:yy [/c "comment"]] 没有参数 显示帮助。这与键入 /? 是一样的。
/? 显示帮助。这与不键入任何选项是一样的。
/i 显示图形用户界面(GUI)。
这必须是第一个选项。
/l 注销。这不能与 /m 或 /d 选项一起使用。
/s 关闭计算机。
/r 关闭并重新启动计算机。
/g 关闭并重新启动计算机。系统重新启动后,
重新启动所有注册的应用程序。
/a 中止系统关闭。
这只能在超时期间使用。
/p 关闭本地计算机,没有超时或警告。
可以与 /d 和 /f 选项一起使用。
/h 休眠本地计算机。
可以与 /f 选项一起使用。
/e 记录计算机意外关闭的原因。
/m \\computer 指定目标计算机。
/t xxx 设置关闭前的超时为 xxx 秒。
有效范围是 0-600,默认为 30。
使用 /t xxx 表示已经设置 /f 选项。
/c "comment" 重启动或关闭的原因的注释。
最多允许 512 个字符。
/f 强制正在运行的应用程序关闭,不前台警告用户。
与 /t xxx 一起使用时 /f 自动设置。
/d [p|u:]xx:yy 提供重新启动或关机的原因。
p 表明重新启动或关闭是计划内的。
u 表示原因由用户定义。
如果 p 和 u 都没有指定,则重新启动或关机是未计划
xx 是主要原因号(小于 256 的正整数)。
yy 是次要原因号(小于 65536 的正整数)。此计算机上的原因:
(E = 预期 U = 意外 P = 计划, C = 自定义)
类别 主要 次要 标题 U 0 0 其他(没有计划的)
E 0 0 其他(没有计划的)
E P 0 0 其他(计划的)
U 0 5 其他故障: 系统没有反应
E 1 1 硬件: 维护(没有计划的)
E P 1 1 硬件: 维护(计划的)
E 1 2 硬件: 安装(没有计划的)
E P 1 2 硬件: 安装(计划的)
P 2 3 操作系统: 升级(计划的)
E 2 4 操作系统: 重新配置(没有计划的)
E P 2 4 操作系统: 重新配置(计划的)
P 2 16 操作系统: Service Pack (计划中)
2 17 操作系统: 热修补(没有计划)
P 2 17 操作系统: 热修补(计划中)
2 18 操作系统: 安全修补(没有计划)
P 2 18 操作系统: 安全修补(计划中)
E 4 1 应用程序: 维护(没有计划的)
E P 4 1 应用程序: 维护(计划的)
E P 4 2 应用程序: 安装(计划中)
E 4 5 应用程序: 没有反应
E 4 6 应用程序: 不稳定
U 5 15 系统故障: 停止错误
E 5 19 安全问题
U 5 19 安全问题
E P 5 19 安全问题
E 5 20 网络连接性丢失(没有计划的)
U 6 11 电源故障: 电线被拔掉
U 6 12 电源故障: 环境
P 7 0 旧版 API 关机
-------------------------------
另外你也可以掉用API关机.
不过我发誓你肯定觉得那比shutdown更麻烦...
//------------关机方法
public void guanji()
{
try
{
//启动本地程序并执行命令
Process.Start("Shutdown.exe", " -s -t 0");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//----------重启重启
public void chongqi()
{
try
{
//启动本地程序并执行命令
Process.Start("shutdown.exe"," -r -t 0");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}//赠送一个使用win32API函数注销的 //===================================================================================注销 函数 声明
[DllImport("user32.dll", EntryPoint = "ExitWindowsEx", CharSet = CharSet.Ansi)]
//ExitWindowsEx 函数
private static extern int ExitWindowsEx(int uFlags, int dwReserved);
//======================================================================================
public void zhuxiao() //注销
{
ExitWindowsEx(0, 0);
}
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;namespace WindowsApplication1
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container 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.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(24, 24);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "关机";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(112, 24);
this.button2.Name = "button2";
this.button2.TabIndex = 1;
this.button2.Text = "注销";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(208, 24);
this.button3.Name = "button3";
this.button3.TabIndex = 2;
this.button3.Text = "重新启动";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 70);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} #region 微软提供的关机接口 调用系统的 kernel32.dll advapi32.dll user32.dll 实现的关机
[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 DoFlag, 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.Button button2;
private System.Windows.Forms.Button button3;
internal const int EWX_FORCEIFHUNG = 0x00000010; private static bool DoExitWin(int DoFlag)
{
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(DoFlag, 0);
return ok;
} /**//// <summary>
/// 重新启动
/// </summary>
public static bool Reboot()
{
return DoExitWin(EWX_FORCE | EWX_REBOOT);
} /**//// <summary>
/// 关机
/// </summary>
public static bool PowerOff()
{
return DoExitWin(EWX_FORCE | EWX_POWEROFF);
} /**//// <summary>
/// 注销
/// </summary>
public static bool LogOff()
{
return DoExitWin(EWX_FORCE | EWX_LOGOFF);
}
#endregion private void button2_Click(object sender, System.EventArgs e)
{
LogOff();
} private void button1_Click(object sender, System.EventArgs e)
{
PowerOff();
} private void button3_Click(object sender, System.EventArgs e)
{
Reboot();
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;namespace A
{
public class Shudown
{
[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 DoFlag, 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;
internal const int EWX_FORCEIFHUNG = 0x00000010; private static void DoExitWin(int DoFlag)
{
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(DoFlag, 0);
} public static void Reboot()
{
DoExitWin(EWX_FORCE | EWX_REBOOT);
} public static void PowerOff()
{
DoExitWin(EWX_FORCE | EWX_POWEROFF);
} public static void LogOff()
{
DoExitWin(EWX_FORCE | EWX_LOGOFF);
}
}
}
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 DoFlag, 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;
internal const int EWX_FORCEIFHUNG = 0x00000010; private static void DoExitWin(int DoFlag)
{
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(DoFlag, 0);
} public static void Reboot()
{
DoExitWin(EWX_FORCE | EWX_REBOOT);
} public static void PowerOff()
{
DoExitWin(EWX_FORCE | EWX_POWEROFF);
} public static void LogOff()
{
DoExitWin(EWX_FORCE | EWX_LOGOFF);
}
}
p.StartInfo.Arguments = "/c shutdown.exe -r -t 0";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true; p.Start();
p.StandardInput.WriteLine("exit");
private void button1_Click(object sender, EventArgs e)//关机
{
Process process = new Process();
process.StartInfo.FileName = "shutdown.exe";
process.StartInfo.Arguments = "-s -t 0";
process.Start(); } private void button2_Click(object sender, EventArgs e)//重启
{
Process process = new Process();
process.StartInfo.FileName = "shutdown.exe";
process.StartInfo.Arguments = "-r -t 0";
process.Start();
}
自己找到了它的System.Diagnostics 命名空间 感谢这位兄弟了 !
API还没接触过。