tags:

views:

450

answers:

3
+3  A: 

The best way to write an auto updater is to have a secondary application. The first program calls the second with elevated privileges, prompting UAC. Then the second application can install the patches.

Daniel A. White
Or, make the application call itself again with elevated privileges. If it doesn't have those privileges, it should run itself again, prompting the UAC for more rights...
Workshop Alex
That is exactly what I'm doing right now, but that still doesn't answer my question : why is SecurityManager.IsGranted return TRUE for this folder if in reality it is not granted for the program files folder?
Eric Girard
+1  A: 

The important thing to know about UAC is that by default, no code runs with Administrator privileges and thus cannot write to the Program Files directory. Even if you are logged in as an administrator, the apps are launched with standard user privliges.

There are two ways around this. You can have the user start the app with the Run As Administrator menu item. But this relies on the user to remember something. The better was is to embed a manifest into your executable that requests administrator privileges. In the manifest, set requestedExecutionLevel to requireAdministrator. This will cause UAC to prompt the user for admin credentials as soon as the app starts.

As Daniel said, the best solution is to put the updating functionality in a separate application. Your primary app will have an manifest that sets the requestedExecutionLevel to "asInvoker" and your updater app with request "requireAdministrator". Your primary app can run with standard privileges. But when the update needs to happen, use Process.Start to launch the updater application that requires the user to enter the admin credentials.

epotter
A: 

I'm not sure if this is what you're trying to do, but I've found this post helpful. The included code let's you detect if you're app is running on Vista, if UAC is enabled and if user is elevated.

http://www.itwriting.com/blog/198-c-code-to-detect-uac-elevation-on-vista.html

then restart your app with runas to let user elevate permissions

ProcessStartInfo processInfo = new ProcessStartInfo();
processInfo.Verb = "runas";
processInfo.FileName = Application.ExecutablePath;
Process.Start(processInfo);
Slav