Logo
HOWTO: Launch a process with admin rights from a Visual Studio add-in on Windows Vista or higher.

Author: Carlos J. Quintero (Microsoft MVP) Applies to: Microsoft Visual Studio 2005
Date: April 2009   Microsoft Visual Studio 2008
Updated: March 2013   Microsoft Visual Studio 2010
      Microsoft Visual Studio 2012
Introduction

While on Windows XP by default every user is an administrator (although you can create "standard" users) Windows Vista introduces a new feature named User Account Control (UAC) which causes that by default even administrators run as standard users. When a process requires administrator rights to run, the operating system prompts for an "elevation prompt".

You can learn more about the UAC feature in the following pages:

Understanding and Configuring User Account Control in Windows Vista

UAC: Standard User Changes

Visual Studio .NET 2002, 2003 and 2005 were designed to run with administrator rights so you get an "elevation prompt" when running on Windows Vista. However, Visual Studio 2008 and higher was designed to run without administrator rights so no "elevation prompt" appears on Windows Vista when launching it. This means that add-ins running inside Visual Studio 2008 are running also without administrator rights and require some considerations to run correctly as explained in the article HOWTO: Design a Visual Studio add-in to install and run on Windows Vista / 7.

This also means that if an add-in needs to launch a process that requires administrator rights (such as regedit.exe or others), it needs to take into account the UAC feature. This article explains how to do it.

More Information

To launch a process with administrator rights from a process that lacks them (such as a Visual Studio 2008 add-in), you can use the following code:

Language: VB.NET   Copy Code Copy Code (IE only)
Dim process As System.Diagnostics.Process = Nothing
Dim processStartInfo As System.Diagnostics.ProcessStartInfo

processStartInfo = New System.Diagnostics.ProcessStartInfo()

processStartInfo.FileName = "regedit.exe"

If System.Environment.OSVersion.Version.Major >= 6 Then ' Windows Vista or higher
   processStartInfo.Verb = "runas"
Else
   ' No need to prompt to run as admin
End If

processStartInfo.Arguments = ""
processStartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal
processStartInfo.UseShellExecute = True

Try
   process = System.Diagnostics.Process.Start(processStartInfo)
Catch ex As Exception
   MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally

   If Not (process Is Nothing) Then
      process.Dispose()
   End If

End Try
Language: C#   Copy Code Copy Code (IE only)
System.Diagnostics.Process process = null;
System.Diagnostics.ProcessStartInfo processStartInfo;

processStartInfo = new System.Diagnostics.ProcessStartInfo();

processStartInfo.FileName = "regedit.exe";

if (System.Environment.OSVersion.Version.Major >= 6)  // Windows Vista or higher
{
   processStartInfo.Verb = "runas";
}
else
{
   // No need to prompt to run as admin
}

processStartInfo.Arguments = "";
processStartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
processStartInfo.UseShellExecute = true;

try
{
   process = System.Diagnostics.Process.Start(processStartInfo);
}
catch (Exception ex)
{
   MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
   if (process != null)
   {
      process.Dispose();
   }
}

Related articles



Go to the 'Visual Studio Extensibility (VSX)' web site for more articles like this (Articles section)


Top