Authenticate as administrator?
I'm developing an installer, and I would like to write to a folder accessible only to administrators. So, I need to displa开发者_如何学Pythony the UAC dialog and authenticate as an administrator. I know I could do this by having a separate executable that I run from the installer GUI, but I'd rather a solution that allows me to authenticate whenever I want in the application. Is this possible in .NET?
You can not change privilege level mid process, once a process has started it is stuck at that privilege level. You must launch a separate process with elevated privileges and communicate with it with IPC to update the GUI.
If you are writing a installer the best thing is to just mark the installers own application manifest as administrative rights itself so it will request admin privileges when it starts as you will need it every time anyway.
You must run a new process with this configuration for the Manifest
file :
<security>
<requestedPrivileges>
<requestedExecutionLevel level="requireAdministrator" />
</requestedPrivileges>
</security>
As an alternative to starting a new process elevated, you can create a new COM object elevated.
Some installers use an approach similar to what you're trying to do. An example to this would be foobar2000 installer. At the first screen it asks where it would a standard (all users) or portable setup. If standard is selected, it prompts for elevation and relaunches itself.
So your simplest bet would be to collect data from user, then relaunch itself or another executable as Administrator to do the actual job passing it parameters with user's selections.
精彩评论