C#操作电脑重启
private const int EWX_SHUTDOWN = 0x00000001;
private const int EWX_REBOOT = 0x00000002;
private const int EWX_FORCE = 0x00000004;
pr
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; private const int EWX_FORCEIFHUNG = 0x00000010; private static void Reboot(bool force) { try { if (force) { DoExitWin(EWX_REBOOT | EWX_FORCE); } else { DoExitWin(EWX_REBOOT | EWX_FORCEIFHUNG); } } catch (Exception ex) { Helps.wlog("关机或重启电脑失败:"+ex.Message,1); } } public static void DoExitWin(int flg) { //give current process SeShutdownPrivilege TokPriv1Luid tp; IntPtr hproc = GetCurrentProcess(); IntPtr htok = IntPtr.Zero; if (!OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok)) { throw new Exception("Open Process Token fail"); } tp.Count = 1; tp.Luid = 0; tp.Attr = SE_PRIVILEGE_ENABLED; if (!LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid)) { throw new Exception("Lookup Privilege Value fail"); } if (!AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero,IntPtr.Zero)) { throw new Exception("Adjust Token Privileges fail"); } //Exit windows if (!ExitWindowsEx(flgwindows重启, 0)) { throw new Exception("Exit Windows fail"); } } (编辑:92站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |