-3

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

TRINIOX
  • 19
  • 5
  • 1
    Don't understand, is this programming related? As in run your program in admin or work related...? – penleychan Dec 10 '18 at 20:26
  • What does this have to with coding or C#? From the wording, this sounds as though you're wanting to ask your administrator if you may have administrative access to run a program. In this case, this is off-topic for this webstie – Jaskier Dec 10 '18 at 20:29
  • If it's just a matter of allowing admins to run with elevation and non-admins to run without elevation, this might help: https://stackoverflow.com/q/8671579 – madreflection Dec 10 '18 at 20:34

2 Answers2

5

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.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
1

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.

Xiaoy312
  • 14,292
  • 1
  • 32
  • 44