How to force a user to a user administrator account in WinForms

I have a simple WinForms application that modifies the Windows registry. The problem is that in Vista / Windows 7 I need to get the user to switch to administrator.

I do not want to force the user to run as the start of the form for the administrator. I want him to do this when there is a need to write to the registry.

The best scenario would be to achieve the same message that appears in many installations when the user needs to "switch" to "Administrator", so there is no need to run the administrator as a form.

How can I achieve this in .Net?

+6
c # windows windows-7 winforms
source share
3 answers

Separation is the way to go if the application sometimes does not execute the registry, and sometimes it does. Three keys to the separation: (1) have a manifest on the second exe, as Ho says, (2) put the screen on the button / menu item so that the user expects an elevation, and (3) launch it using ShellExecute (before calling Start, set to UseShellExecuteFlag (true) to use the manifest.

However, before solving the problem of breaking down the application, I would ask two questions. Firstly, is it ever used for non-administrative purposes or does each user always “click this button” and have to pick it up? If so, just put the admin manifest in the application and do not separate it. Secondly, you definitely need to write this part of the registry? Could you move your key to something under HKCU? If you can, then you no longer need the hill, and all is happier. I always like these features first, as they mean less code and fewer tests than partioning.

+4
source share

As Aaronok says, I do not think that the process may require elevation. One way is to split your process into two applications, one of them is the usual one that does most of the work, and the other only writes the registry, and this one has a manifest that contains something like

<requestedExecutionLevel level="requireAdministrator"/> 
+2
source share

As far as I know, there is no API for raising the process. This happens automatically when a process tries to start another process in elevated mode.

This also works with the Windows installer. I'm not sure if it literally starts another elevated process or just creates an elevated COM object, but this is actually the same.

I personally would not resort to this hacker round to raise your process in the middle of execution; if your process may require elevation, then make it explicit with a manifest and let the consent message appear at startup. But if you absolutely must do this, then how to do it - you need to start the elevated process from your application.

+1
source share

All Articles