C# run process as administrator without prompt

To run a process as administrator without a prompt in C#, you can set the process start info’s ‘Verb’ property to ‘runas’. This will ensure that the process is launched with administrator privileges. Here’s an example to demonstrate this:


using System;
using System.Diagnostics;

namespace ProcessExample
{
class Program
{
static void Main(string[] args)
{
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "notepad.exe";
psi.Verb = "runas"; // Run as administrator

try
{
Process.Start(psi);
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}
}
}

In the above example, we create a new instance of the ‘ProcessStartInfo’ class and set the ‘FileName’ property to the path of the executable we want to run with administrator privileges (in this case, ‘notepad.exe’). The ‘Verb’ property is set to ‘runas’, which specifies that the process should be launched with administrator rights.

Inside the ‘try’ block, we call the ‘Process.Start’ method with the created ‘ProcessStartInfo’ object. If any error occurs during the process start, it will be caught in the ‘catch’ block, and an error message will be displayed.

You can modify the ‘FileName’ property to the path of the application you want to run with administrator privileges. Make sure to handle any exceptions that might occur during the process start.

Read more

Leave a comment