Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleLaunch a program while requesting privilege elevation in Visual Basic 2008
DescriptionThis example shows how to launch a program while requesting privilege elevation in Visual Basic 2008.
KeywordsUAC, shield, Vista, user access control, elevated privileges, Run As, RunAs
CategoriesWindows, Software Engineering
 
To launch the program with privilege elevation, the code creates a ProcessStartInfo object. It sets the object's Verb property to runas to indicate privilege elevation, fills in other fields such as the executable's name, and uses Process.Start to launch the proces.

If the user doesn't enter an administrator password, Process.Start throws an error so the code uses a Try Catch block to watch for this.

The program waits for the elevated process to finish and displays a Done message.

 
' Prepare to start.
Dim psi As New ProcessStartInfo
psi.Verb = "runas"
psi.UseShellExecute = True
psi.FileName = Application.ExecutablePath

Try
    ' Start the process.
    Dim proc As Process = Process.Start(psi)

    ' Wait for the process to finish.
    proc.WaitForExit()

    MessageBox.Show("Done")
Catch ex As Exception
    MessageBox.Show(ex.Message)
End Try
 
Note that if you click the button while running the program with elevated privileges, the standard UAC privilege elevation dialog does not appear. Instead the new instance of the program runs with inherited elevated privileges.
 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated