Click here to Skip to main content
15,902,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am wondering how to run codes after uac self elevation.
basically when i click a button once it elevated it suppose to add a key to the registry. so far i manage to get the self elevation part and the adding the registry part. now i cannot seem to make it run in a single click.

from my research the self elevation code are running another instance and closing the the current one. but when that happen that new instance does not continue the code. is there a work around for it.
VB
Dim proc As New ProcessStartInfo

        proc.UseShellExecute = True
        proc.WorkingDirectory = "c:\debug"
        proc.FileName = "test.exe"
        proc.Verb = "runas"

        If IsRunAsAdmin() = False Then

            Try

                Process.Start(proc)
                Application.Exit()

            Catch
                ' The user refused to allow privileges elevation. 
                MsgBox("Permission denied by user ! Can not proceed.", MsgBoxStyle.Critical)

                Return

            End Try
        Else

            MessageBox.Show("Already Running as Admin")

        End If


    End Sub


VB
Friend Function IsRunAsAdmin() As Boolean

       Dim principal As New WindowsPrincipal(WindowsIdentity.GetCurrent)

       Return principal.IsInRole(WindowsBuiltInRole.Administrator)

   End Function
Posted

1 solution

Why don't you add your required privilege to your application manifest. You need not to perform the above specified check every time.

Check this (Create and Embed an Application Manifest (UAC))[^].
 
Share this answer
 
Comments
theonebit 20-Sep-12 8:46am    
Thank you for your reply. Its a good proposal but my app does not require admin privilages. it only requires it to write to registry. I believe reading the registry does not require admin privilages. Hence, the only time i would ever need the admin privilage is to write my apps serial number to the registry. After that it just reading and verifying.

That is why i am looking for a work around to this situiation. i just need it when i need it. on demand. but the problem lies on the way it it being elevated.

infact i am quite satisfied with what i have got just that i cannot get it to autorun after its elevated.

for example
<pre lang="vb">
If IsRunAsAdmin() = False Then

Try

Process.Start(proc)
Application.Exit()
'now add the registry key here

Catch
' The user refused to allow privileges elevation.
MsgBox("Permission denied by user ! Can not proceed.", MsgBoxStyle.Critical)

Return

End Try
Else

'if already admin just add the key

End If
</pre>

but it wont run since the application have exited and there is a new app running. user can always press the button again but its not smooth transition.

i can add a detection through form load where if the form is loaded and is in admin mode then automatically add the registry key. or create another app for the registring part or built a console. but currently i am looking at is there another way for self elevation on demand?
Malli_S 20-Sep-12 9:00am    
It's bit confusing. Are you launching the same app or some other app to write the entries to registry with elevated rights?

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900