So I basicly want my Application to relaunch itself w/o requesting Administrator Permissions if it has been declined the first time.
edit:
thanks for your anwsers, turns out it's not possible to work the way I wanted it to
So I basicly want my Application to relaunch itself w/o requesting Administrator Permissions if it has been declined the first time.
edit:
thanks for your anwsers, turns out it's not possible to work the way I wanted it to
In Windows, either a process elevates at launch or it doesn't. There is no middle ground. A user declining an elevation prompt means the process will fail to launch at all.
Therefore, the only way to do this is to distribute your application as multiple separate programs, where all of the admin tasks are concentrated within the additional exe file(s) which can run in a separate process. Then, when you need to do an activity as admin, you can launch that program with the appropriate information. Command line arguments, file on disk, or via network or IPC can all work for providing any needed additional information for the supplemental programs. The user will see the prompt for the new process and approve or decline it, and you can detect the decline status by having the application take some visible action when it first starts successfully.
Of course, you still want to attempt as much of the work as you can as a regular user. For example, walking the file system still works as a normal user; you just don't see into certain folders. This does not have to mean duplicating code. You can put your code into a class library (dll), and launch into the same class library from your main (unprivileged) process as the from privileged child process.
Beside explicitly forcing the user to run as admin via requestedExecutionLevel
, you can also use WindowsPrincipal.IsInRole
to check during run-time.
Depending on on the user access level, you can enable/disable certain features. And, you can also remind the user that he can restart the app in admin (right-click > run as administrator) to enable some features at launch or when the disabled feature is presented.