C#
administrator mode
process management
programming
code duplication

How to start a Process as administrator mode in C

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In C#, the usual way to start a process with administrator privileges is to use ProcessStartInfo with UseShellExecute = true and Verb = "runas". That tells Windows to launch the process through the shell and show the normal User Account Control elevation prompt if required.

Start the Process with runas

This is the core pattern:

csharp
1using System;
2using System.Diagnostics;
3
4class Program
5{
6    static void Main()
7    {
8        var startInfo = new ProcessStartInfo
9        {
10            FileName = "notepad.exe",
11            UseShellExecute = true,
12            Verb = "runas"
13        };
14
15        Process.Start(startInfo);
16    }
17}

If the current user approves the prompt, the process starts elevated. If the user cancels it, process creation fails.

Pass Arguments and a Target Executable

You can start your own executable with arguments the same way:

csharp
1using System.Diagnostics;
2
3var startInfo = new ProcessStartInfo
4{
5    FileName = @"C:\Tools\MyApp.exe",
6    Arguments = "--repair --verbose",
7    UseShellExecute = true,
8    Verb = "runas"
9};
10
11Process.Start(startInfo);

The important setting is still UseShellExecute = true. Without that, the Verb is ignored and Windows will not perform shell-based elevation.

Handle User Cancellation Cleanly

Elevation can fail for legitimate reasons, especially when the user clicks "No" in the User Account Control dialog. Catch that case explicitly:

csharp
1using System;
2using System.ComponentModel;
3using System.Diagnostics;
4
5try
6{
7    var startInfo = new ProcessStartInfo
8    {
9        FileName = "cmd.exe",
10        Arguments = "/c echo elevated",
11        UseShellExecute = true,
12        Verb = "runas"
13    };
14
15    Process.Start(startInfo);
16}
17catch (Win32Exception ex)
18{
19    Console.WriteLine($"Process was not started: {ex.Message}");
20}

That avoids an unhelpful crash when elevation is denied.

Understand What Elevation Does Not Do

Starting a child process as administrator does not retroactively elevate your current process. Your current application stays at its original privilege level unless it was itself launched with elevation.

That matters when developers expect one elevated helper process to somehow grant admin rights back to the parent. Windows does not work that way. If the current app needs elevated behavior for its own operations, it must be launched accordingly or declare the requirement in its manifest.

Consider Whether You Need Elevation at All

Elevation should be the exception, not the default. Use it only for operations that genuinely require administrator access, such as writing to protected locations, editing system settings, or installing software.

If only one step requires elevated access, a good design is to keep the main application unelevated and launch a focused helper process with runas for that step. That reduces risk and keeps the normal UX cleaner.

Use a Manifest Only When the Whole App Must Elevate

If your application always needs administrator rights, another option is an application manifest with a requested execution level. That changes the elevation model for the whole app, whereas Verb = "runas" is better when you only need to elevate a specific child process occasionally.

Common Pitfalls

  • Forgetting UseShellExecute = true. Without it, Verb = "runas" does not trigger shell elevation.
  • Assuming the current process becomes elevated after launching one child process as administrator. Elevation applies only to the new process.
  • Not handling Win32Exception when the user cancels the UAC prompt.
  • Elevating every launch by default when only a small subset of tasks actually needs admin rights.
  • Passing a file path or arguments incorrectly and then blaming elevation. Always verify the target executable and argument string first.

Summary

  • In C#, start an elevated process with ProcessStartInfo, UseShellExecute = true, and Verb = "runas".
  • Use the pattern for a specific executable and optional arguments.
  • Catch cancellation errors so the app fails gracefully when UAC is denied.
  • Remember that only the child process becomes elevated.
  • Request administrator privileges only for operations that truly need them.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.