|
|
Title | Launch a program while requesting privilege elevation in Visual Basic 2008 |
Description | This example shows how to launch a program while requesting privilege elevation in Visual Basic 2008. |
Keywords | UAC, shield, Vista, user access control, elevated privileges, Run As, RunAs |
Categories | Windows, 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.
|
|
|
|
|
|