コード例 #1
0
        public static void RunWithPrivilege(string privilege, bool enabled, PrivilegedCallback callback, object state)
        {
            if (callback == null)
            {
                throw new ArgumentNullException("callback");
            }

            using (TokenPrivilege p = new TokenPrivilege(privilege))
            {
                RuntimeHelpers.PrepareConstrainedRegions();
                try
                {
                    if (enabled)
                    {
                        p.Enable();
                    }
                    else
                    {
                        p.Disable();
                    }

                    callback(state);
                }
                finally
                {
                    p.Revert();
                }
            }
        }
コード例 #2
0
        public static void RunWithPrivilege(string privilege, bool enabled, PrivilegedCallback callback, object state)
        {
            if (callback == null)
            {
                throw new ArgumentNullException("callback");
            }

            using(TokenPrivilege p = new TokenPrivilege(privilege))
            {
                RuntimeHelpers.PrepareConstrainedRegions();
                try
                {
                    if (enabled)
                    {
                        p.Enable();
                    }
                    else
                    {
                        p.Disable();
                    }

                    callback(state);
                }
                finally
                {
                    p.Revert();
                }
            }
        }