Ntohing_Trolll Ntohing_Trolll - 1 year ago 94
Vb.net Question

how to elevate administrator pirivileges only when required vb.net

I want to make an application with self-update function.

Unfortunately, when I want to click on the button called "Update" it throws an error, with the message: "Access Denied!"

Here is the code I used:

Public Sub versionfileUPDATE()
If System.IO.File.Exists(My.Settings.telepitesihely & "/seemtahelperv1downloader.exe") Then
System.IO.File.Delete(My.Settings.telepitesihely & "/seemtahelperv1downloader.exe")
client.DownloadFileAsync(New Uri("https://dl.dropboxusercontent.com/s/azz6qundbe2rna1/SeeMta%20PK%20Generator%20V1.exe?dl=11"), My.Settings.telepitesihely & "/seemtahelperv1downloader.exe")
client.DownloadFileAsync(New Uri("https://dl.dropboxusercontent.com/s/azz6qundbe2rna1/SeeMta%20PK%20Generator%20V1.exe?dl=1"), My.Settings.telepitesihely & "/seemtahelperv1downloader.exe")
End If
Catch ex As Exception
End Try
End Sub

So, is there any solution to require administrator privilege, when the user clicks on the update button?

Answer Source

Processes must be given an administrator token when they start or they never will get one at all. The only way to make it work "on demand" is you must make a 2nd program that requires administrator privlages that does the download and nothing else, you then have your program launch the 2nd program and wait for it to finish.

However, the better solution is choose a better save location that does not require administrator privileges to write to, for example you can use Environment.GetFolderPath to find the user's LocalApplicationData folder (for example for you that would point to somthing like C:\Users\Ntohing_Trolll\AppData\Local) and save and run the exe from a subfolder in there. That is what programs like Chrome do for their updating so you don't require administrative privileges to update.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download