Ver versión en español    
 
Home
10 Reasons to use MZ-Tools
MZ-Tools 6.0 for VS.NET
Editions
Features
Online Documentation
MZ-Tools SDK
Download
Purchase
Version History (RSS)  
FAQ & Support
MZ-Tools 3.0 for VB6 & VBA
Features
Online Documentation
Download (freeware)
Donations (Paypal)
Version History (RSS)  
FAQ & Support
User Reviews
Community Place
Contact  
For Add-In Developers
About
   
 
User Testimonials

I'm an avid supporter of MZ-Tools. It's a product I couldn't do without and your level of support is outstanding.

Jan Hyde (Visual Basic MVP)

You will soon wonder how you ever lived without it.

Andy Maggs

More user reviews
 
HOWTO: Launch a process with admin rights from a Visual Studio 2008 add-in on Windows Vista.

Author: Carlos J. Quintero (Microsoft MVP) Applies to: Microsoft Visual Studio 2008
Date: April 2009    
       
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 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.

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 back to the 'Resources for Visual Studio .NET extensibility' section for more articles like this


MZ-Tools 6.0 for Visual Studio .NET

You can code, design, locate code and document your apps much faster using VB.NET, C#, C++ or Visual J#:

Buy MZ-Tools Now Download MZ-Tools Demo

   Top