Click here to Skip to main content
13,298,965 members (37,670 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


11 bookmarked
Posted 19 Feb 2013

Elevate your application at start up

, 26 Feb 2013
Rate this:
Please Sign up or sign in to vote.
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.

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

  If sArgs.Contains("-runasadmin") Then
    ' Running with elevated privileges
    ' 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.

      ' Sleep until the process has completed.
    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

. Start the process. Idle until the child process completes.


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


About the Author

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

You may also be interested in...


Comments and Discussions

SuggestionUse Manifest... Pin
fmaeseel26-Feb-13 20:47
memberfmaeseel26-Feb-13 20:47 
AnswerRe: Use Manifest... Pin
Adam Zuckerman27-Feb-13 5:04
memberAdam Zuckerman27-Feb-13 5:04 
GeneralRe: Use Manifest... Pin
P.Venkatachalam5-Mar-13 0:00
memberP.Venkatachalam5-Mar-13 0:00 
GeneralMy vote of 4 Pin
Klaus Luedenscheidt26-Feb-13 20:40
memberKlaus Luedenscheidt26-Feb-13 20:40 
I think you should file this also under Tips/Tricks.

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171207.1 | Last Updated 26 Feb 2013
Article Copyright 2013 by Adam Zuckerman
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid