コード例 #1
0
 public void Dispose()
 {
     // disables any privileges that were enabled by this class
     if (previousState != null)
     {
         PrivilegeUtil.SetTokenPrivileges(process, previousState);
     }
     GC.SuppressFinalize(this);
 }
コード例 #2
0
 /// <summary>
 /// Temporarily enables the privileges specified and reverts once the class is disposed.
 /// </summary>
 /// <param name="strict">Whether to fail if any privilege failed to be enabled, if false then this will continue silently</param>
 /// <param name="privileges">A list of privileges to enable</param>
 public PrivilegeEnabler(bool strict, params string[] privileges)
 {
     if (privileges.Length > 0)
     {
         process = PrivilegeUtil.GetCurrentProcess();
         Dictionary <string, bool?> newState = new Dictionary <string, bool?>();
         for (int i = 0; i < privileges.Length; i++)
         {
             newState.Add(privileges[i], true);
         }
         try
         {
             previousState = PrivilegeUtil.SetTokenPrivileges(process, newState, strict);
         }
         catch (Win32Exception e)
         {
             throw new Win32Exception(e.NativeErrorCode, String.Format("Failed to enable privilege(s) {0}", String.Join(", ", privileges)));
         }
     }
 }