/// <summary> /// 获取更高级别的操作系统权限。 /// </summary> /// <param name="privilege">需要获得的权限。</param> /// <exception cref="Win32ApiErrorInformationException">当出现未知的或者无法处理的Win32Api错误代码时,则需要抛出这个异常。</exception> public static void GetSystemAuthority(string privilege) { if (!Win32ApiHelper.CheckEntryPoint("advapi32.dll", "AdjustTokenPrivileges")) { return; } IntPtr tokenHandle = IntPtr.Zero; SLocallyUniqueIdentifier luid = new SLocallyUniqueIdentifier(); STokenPrivileges newPrivileges = new STokenPrivileges(); STokenPrivileges tokenPrivileges; if (!OpenProcessToken(Process.GetCurrentProcess().Handle, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref tokenHandle)) { throw new Win32ApiErrorInformationException(); } if (LookupPrivilegeValue(null, privilege, ref luid) == 0) { throw new Win32ApiErrorInformationException(); } tokenPrivileges.PrivilegeCount = 1; tokenPrivileges.Privileges.Attributes = SE_PRIVILEGE_ENABLED; tokenPrivileges.Privileges.ParticularLuid = luid; int size = 4; if (AdjustTokenPrivileges(tokenHandle, 0, ref tokenPrivileges, 4 + (12 * tokenPrivileges.PrivilegeCount), ref newPrivileges, ref size) == 0) { throw new Win32ApiErrorInformationException(); } }