加入收藏 | 设为首页 | 会员中心 | 我要投稿 开发网_商丘站长网 (https://www.0370zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 服务器 > 搭建环境 > Windows > 正文

C#操作电脑重启

发布时间:2022-11-25 23:04:14 所属栏目:Windows 来源:未知
导读: private const int EWX_LOGOFF = 0x00000000;
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");

}

}

(编辑:开发网_商丘站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!