Administrator permission for part of C # application

I am writing a C # GUI application on VS10. One of its necessary functions is to check the contents of a specific file, and if it needs to be updated, it must be updated in administrator mode. A letter in the manifest file causes the application to run in administrator mode regardless of the contents of the file, which is undesirable (simply because it is a pain). Is there a way to call admin mode at run time and only if necessary? Thanks!

+4
source share
2 answers

Unfortunately, you cannot escalate at run time.

To achieve the same goal, separate the code that updates your file into your own executable file, which has administrator access through its manifest.

Running this application from the main application allows you to request administrator access when it is needed , without having to unnecessarily grant permissions to the rest of your code.

+4
source

I believe the solution is to have the application restart in administrator mode if necessary.

A quick google shows:

From CodeProject

But I agree with @WillEddins answer ... it would be โ€œbetterโ€ (if possible / practical) to split the โ€œmodeโ€ code of the administrator into another executable file. I assume that this will depend (among other things, for example, on efforts / costs / benefits / risks) on how closely the administrator functions are integrated, mixed with functions other than the administrator.

+2
source

Source: https://habr.com/ru/post/1431998/


All Articles