Click here to Skip to main content
15,867,453 members
Articles / UAC

Elevate your application at start up

Rate me:
Please Sign up or sign in to vote.
4.67/5 (3 votes)
26 Feb 2013CPOL1 min read 12.7K   11   4
Sometimes you need to run your application with elevated privileges to get past issues with UAC.  

Sometimes you need to run your application with elevated privileges to get past issues with UAC.  If you need to insure that your application is always run with administrator privileges, this little bit of code can help.

VB.NET
Sub Main()
  Dim sArgs As String() = System.Environment.GetCommandLineArgs()

  If sArgs.Contains("-runasadmin") Then
    ' Running with elevated privileges
    SomeMethod()
  Else
    ' Re-launch the current application with elevated privileges
    ReDim Preserve sArgs(sArgs.Length + 1)
    sArgs(sArgs.Length - 1) = "-runasadmin"

    Dim oProcessStartInfo As System.Diagnostics.ProcessStartInfo = _
                             New ProcessStartInfo( _
                                 Reflection.Assembly.GetEntryAssembly().Location, _
                                 Join(sArgs, " "))
    oProcessStartInfo.Verb = "runas"

    Dim oProcess As New System.Diagnostics.Process()
    With oProcess
      ' Enable the WaitForExit method
      .EnableRaisingEvents = True
      .StartInfo = oProcessStartInfo

      ' Start the process.
      .Start()

      ' Sleep until the process has completed.
      .WaitForExit()
    End With
  End If
End Sub

The first thing we do is to get the command line arguments. Why? This is the easiest way to tell our running application that we want it to do something special.

The next thing to do is determine if we are running elevated or not. If we are, let's go do what we are supposed to be doing. The way I used to determine if we are elevated is to add a command line argument of -runasadmin. Please be aware that this is not something added by Windows, but manually by my code.

If the code is not running elevated, then let's re-launch the application with administrator privileges. This is accomplished by first adding -runasadmin to the existing command line arguments. Create an System.Diagnostics.ProcessStartInfo object that will tell our application to request the administrator privileges. Adding the verb runas to the System.Diagnostics.ProcessStartInfo is what does the magic. Create a new System.Diagnostics.Process, add the

System.Diagnostics.ProcessStartInfo
. Start the process. Idle until the child process completes.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
Long time software engineer who rambles occasionally about coding, best practices, and other random things.

Comments and Discussions

 
SuggestionUse Manifest... Pin
fmaeseel26-Feb-13 19:47
fmaeseel26-Feb-13 19:47 
AnswerRe: Use Manifest... Pin
Adam Zuckerman27-Feb-13 4:04
Adam Zuckerman27-Feb-13 4:04 
This is another way to achieve the same goal.

The technique I show above could be applied to any executable so that you only elevate privileges only when absolutely necessary rather than the whole application.

Each method has its uses.
Long time software engineer who rambles occasionally about coding, best practices, and other random things.
http://justsomevbcode.blogspot.com/

GeneralRe: Use Manifest... Pin
P.Venkatachalam4-Mar-13 23:00
P.Venkatachalam4-Mar-13 23:00 
GeneralMy vote of 4 Pin
Klaus Luedenscheidt26-Feb-13 19:40
Klaus Luedenscheidt26-Feb-13 19:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.