实现XP系统的开关机??? 用c#代码怎么实现XP系统的开关机??? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 using System.Runtime.InteropServices;//用来调用API函数using System.Management;//调用API //==============**********调用API************* [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; internal const int EWX_FORCEIFHUNG = 0x00000010; private bool 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); return ok; }/// <summary> /// 注销计算机 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button47_Click(object sender, System.EventArgs e) { DoExitWin(EWX_LOGOFF); } /// <summary> /// 关闭计算机 /// </summary> private void button46_Click(object sender, System.EventArgs e) { DoExitWin(EWX_SHUTDOWN); } /// <summary> /// 取消操作 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button43_Click(object sender, System.EventArgs e) { this.Close(); } /// <summary> /// 重启计算机 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button45_Click(object sender, System.EventArgs e) { DoExitWin(EWX_REBOOT); } 如果有权限的话,直接用rundll32.exe调用系统命令就可以了http://blog.csdn.net/jinjazz/archive/2008/04/17/2302095.aspx如果没有权限需要先用windows api函数提升关机权限 http://blog.csdn.net/null1/archive/2008/11/03/3208145.aspx Process 一个 shutdown -s -t 0 就行了~~开机呀楼主~~这个没想过,关机以后恐怕 .Net Runtime 就没了,要实现开机的话,起码需要一台服务器,做网络唤醒才行 关机可以用这个,System.diagnostics.process=new system.diagnostics.process();myprocess.startinfo.filename="cmd.exe";myprocess.startinfo.useshellexecute=false;myprocess.startinfo.Redirectstandardlnput=true;myprocess.startinfo.Redirectstandardoutput=true;myprocess.startinfo.RedirectstandardError=true;myprocess.startinfo.Createnowindow=true;myprocess.start();myprocess.standardlnput.writeline("shoutdown-s-t 0");system.diagnostics.process.start("cmd.exe","shutdown-s-t%time%");-s关机-l注销-r重启-t+时间shutdown+你要干什么+-t你要执行的时间 在线求助!字符串转换为datetime时发生语法错误 面向对象综合练习 对象如何表示出来? 返回数组问题 高分求救: windows form 窗口创建子窗口,并设置自窗口里面的某个控件的值, 江湖救急!在线给分! 请教一个简单的编码问题,谢谢 用网页打开EXCEL,WORD,PPT等文档 C# asp.net [求助]用scoket可以在外部网络发数据给内部网络吗? 如何将文本信息也显示出来!!!!!!!!!!! 请教在vs2005中使用reportviewer控件的相关问题?
using System.Runtime.InteropServices;//用来调用API函数
using System.Management;//调用API
//==============**********调用API*************
[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;
internal const int EWX_FORCEIFHUNG = 0x00000010; private bool 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);
return ok;
}
/// <summary>
/// 注销计算机
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button47_Click(object sender, System.EventArgs e)
{
DoExitWin(EWX_LOGOFF);
}
/// <summary>
/// 关闭计算机
/// </summary>
private void button46_Click(object sender, System.EventArgs e)
{
DoExitWin(EWX_SHUTDOWN);
}
/// <summary>
/// 取消操作
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button43_Click(object sender, System.EventArgs e)
{
this.Close();
}
/// <summary>
/// 重启计算机
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button45_Click(object sender, System.EventArgs e)
{
DoExitWin(EWX_REBOOT);
}
http://blog.csdn.net/jinjazz/archive/2008/04/17/2302095.aspx如果没有权限需要先用windows api函数提升关机权限
System.diagnostics.process=new system.diagnostics.process();
myprocess.startinfo.filename="cmd.exe";
myprocess.startinfo.useshellexecute=false;
myprocess.startinfo.Redirectstandardlnput=true;
myprocess.startinfo.Redirectstandardoutput=true;
myprocess.startinfo.RedirectstandardError=true;
myprocess.startinfo.Createnowindow=true;
myprocess.start();
myprocess.standardlnput.writeline("shoutdown-s-t 0");
system.diagnostics.process.start("cmd.exe","shutdown-s-t%time%");
-s关机
-l注销
-r重启
-t+时间
shutdown+你要干什么+-t你要执行的时间