Elevating process privilege programmatically? Elevating process privilege programmatically? windows windows

Elevating process privilege programmatically?


You can indicate the new process should be started with elevated permissions by setting the Verb property of your startInfo object to 'runas', as follows:

startInfo.Verb = "runas";

This will cause Windows to behave as if the process has been started from Explorer with the "Run as Administrator" menu command.

This does mean the UAC prompt will come up and will need to be acknowledged by the user: if this is undesirable (for example because it would happen in the middle of a lengthy process), you'll need to run your entire host process with elevated permissions by Create and Embed an Application Manifest (UAC) to require the 'highestAvailable' execution level: this will cause the UAC prompt to appear as soon as your app is started, and cause all child processes to run with elevated permissions without additional prompting.

Edit: I see you just edited your question to state that "runas" didn't work for you. That's really strange, as it should (and does for me in several production apps). Requiring the parent process to run with elevated rights by embedding the manifest should definitely work, though.


This code puts the above all together and restarts the current wpf app with admin privs:

if (IsAdministrator() == false){    // Restart program and run as admin    var exeName = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;    ProcessStartInfo startInfo = new ProcessStartInfo(exeName);    startInfo.Verb = "runas";    System.Diagnostics.Process.Start(startInfo);    Application.Current.Shutdown();    return;}private static bool IsAdministrator(){    WindowsIdentity identity = WindowsIdentity.GetCurrent();    WindowsPrincipal principal = new WindowsPrincipal(identity);    return principal.IsInRole(WindowsBuiltInRole.Administrator);}// To run as admin, alter exe manifest file after building.// Or create shortcut with "as admin" checked.// Or ShellExecute(C# Process.Start) can elevate - use verb "runas".// Or an elevate vbs script can launch programs as admin.// (does not work: "runas /user:admin" from cmd-line prompts for admin pass)

Update: The app manifest way is preferred:

Right click project in visual studio, add, new application manifest file, change the file so you have requireAdministrator set as shown in the above.

A problem with the original way: If you put the restart code in app.xaml.cs OnStartup, it still may start the main window briefly even though Shutdown was called. My main window blew up if app.xaml.cs init was not run and in certain race conditions it would do this.


According to the article Chris Corio: Teach Your Apps To Play Nicely With Windows Vista User Account Control, MSDN Magazine, Jan. 2007, only ShellExecute checks the embedded manifest and prompts the user for elevation if needed, while CreateProcess and other APIs don't. Hope it helps.

See also: same article as .chm.