Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

administrative priviledge in c#

/*
A more standard approach (but obsolete) to demand in code that the current user has Admin privileges
before running one or more guarded code is to use the PrincipalPermissionAttribute
for the guarded method of your program.

The following is an example :
*/

// NOTE PrincipalPermissionAttribute.PrincipalPermissionAttribute(SecurityAction) is obsolete
[PrincipalPermissionAttribute(SecurityAction.Demand, Role = @"BUILTINAdministrators")]
private static void AdministratorsCode()
{
  Console.WriteLine("Administrators Only");
}

/*
Declare a usage of the System.Security.Principal and System.Security.Permissions namespaces.
Early in your program, before calling a method like AdministratorsCode(), you need to call :
*/

System.AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);

/*
Below is a complete example code :
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Principal;
using System.Security.Permissions;

namespace ConsoleProgramRunAsAdmon
{
    class Program
    {
        [PrincipalPermissionAttribute(SecurityAction.Demand, Role = @"BUILTINAdministrators")]
        private static void AdministratorsCode()
        {
            Console.WriteLine("Administrators Only");
        }

        static void Main(string[] args)
        {
            try
            {
                System.AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
                AdministratorsCode();
            }
            catch(Exception ex)
            {
                Console.WriteLine("An exception occurred : " + ex.Message);
            }

            Console.ReadKey();
        }
    }
}

// Hope that helps
Comment

PREVIOUS NEXT
Code Example
Csharp :: request a pricipal permission 
Csharp :: unity magnetize a 3d object to cursor 
Csharp :: set request size c# 
Csharp :: how to download somthing from one drive unity 
Csharp :: how to round to nearest number in array c# 
Csharp :: disable alt + f4 in c# forms 
Csharp :: unity get distance between line and point 
Csharp :: find gameobject by name in root 
Csharp :: Reading emails from Gmail in C# 
Csharp :: C# How to display text in console 
Csharp :: unity stop object from rotating 
Csharp :: C# get filebase name 
Csharp :: C# how to know if number is even or odd 
Csharp :: c# if statements 
Csharp :: factorial of any number 
Csharp :: c# reflection get property value array 
Csharp :: unity3d gameobject follow path 
Csharp :: c# do while or 
Csharp :: dotnet core encryption and decryption 
Csharp :: provide inject vue 
Csharp :: c# delete item from list 
Csharp :: ascii code c# char 
Csharp :: how to configure visual studio for unity 
Csharp :: math.pow in C# using loop 
Csharp :: c# webclient vs httpclient 
Csharp :: concatenation on different lines in f# 
Csharp :: IOException: Failed to prepare target build directory. Is a built game instance running? UnityEditor.WindowsStandalone.WindowsDesktopStandalonePostProcessor.DeleteDestination (UnityEditor.Modules.BuildPostProcessArgs args) 
Csharp :: c# convert string to base64 string 
Csharp :: erewt 
Csharp :: how to change the color of a textbox with button c# 
ADD CONTENT
Topic
Content
Source link
Name
8+7 =