Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

c# run as administrator

Create an "Application Manifest File" and change the following line from:
 <requestedExecutionLevel level="asInvoker" uiAccess="false" />

to:
 <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
Comment

how to run a function as administrator in c#

[PrincipalPermission(SecurityAction.Demand, Role = @"BUILTINAdministrators")]
public void MyMethod()
{
  
}
Comment

c# run program as an administrator

// Program

        public static bool IsAdministrator()
        {
                WindowsIdentity identity = WindowsIdentity.GetCurrent();
                WindowsPrincipal principal = new WindowsPrincipal(identity);
                return principal.IsInRole(WindowsBuiltInRole.Administrator);
        }


// Form1

        private void Form1Shown(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            this.Run1();
            this.Cursor = Cursors.Default;
        }

        private void Run1()
        {
            if (!Program.IsAdministrator())
            {
                // Restart and run as admin
                var exeName = Process.GetCurrentProcess().MainModule.FileName;
                ProcessStartInfo startInfo = new ProcessStartInfo(exeName);
                startInfo.Verb = "runas";
                startInfo.Arguments = "restart";
                Process.Start(startInfo);
                Application.Exit();
            }
		}
Comment

PREVIOUS NEXT
Code Example
Csharp :: byte to stream c# 
Csharp :: load prefab in script unity 
Csharp :: application server types in .net 
Csharp :: unity c# class addition syntax 
Csharp :: how to say "Hello world" in c# 
Csharp :: turtle graphics face in direction 
Csharp :: c# memorystream to byte array 
Csharp :: c# console writeline array 
Csharp :: how to draw over label C# 
Csharp :: c# winforms textbox readonly 
Csharp :: c# filter non alphanumeric characters 
Csharp :: c# unity 2d play video 
Csharp :: get the current directory in unity 
Csharp :: check if panel has controler c# 
Csharp :: change picturebox image c# 
Csharp :: c# two different random numbers 
Csharp :: c# dynamic object get value 
Csharp :: cinemachine namespace not working 
Csharp :: transform.rotate unity 
Csharp :: wpf update listview itemssource 
Csharp :: c# tryparse int 
Csharp :: c# read from file 
Csharp :: unity hide in inspector 
Csharp :: c# winforms textbox cursor position 
Csharp :: abril modal boostrap 
Csharp :: ubuntu: how to open the terminal from c# 
Csharp :: pyqt qtableview get selected row data 
Csharp :: unity new Color() 
Csharp :: how to sort string array alphabetically in c# 
Csharp :: write text files with C# 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =