0

i know that i can run app as admin by adding this line

 <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />

but I want the application to run as normal and Only when I click on a button to do something will the user request access

An Android-like device that doesn't have access at run-time but requests access when you save a file

Osman
  • 1,270
  • 2
  • 16
  • 40

1 Answers1

3

In Windows, a process cannot elevate after launch. Full stop.

You have five options, roughly ordered from worst to best:

  1. The process runs as administrator all the time, which you've already said you don't want.
  2. You trust the user to know when they'll need administrator privileges and run accordingly.
  3. Enhance your installer to require administrator access in order to minimize needed administrator privileges later. For example, the installer can change the permissions on a shared folder so non-privileged users can access it.
  4. Your application bundles two separate programs, where tasks which require administrator privileges are moved to the second program, and you build a way for the first program to trigger the correct task with the correct arguments in the second program and know when/if the task completes. In other words, you fake it.
  5. Design your application to not need administrator access in the first place. Two examples: One, you should never need write access to the Program Files folder. This is a bad habit left over from the Windows 98 days, and in pretty much every case the Application Data folder should be used instead. Two, instead of a traditional deployment to the Program Files folder (which requires administrator access to do program updates), you can use something like ClickOnce, which can be run as a non-privileged user.
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • 1
    You can also restart your own app as admin, like [this answer](https://stackoverflow.com/a/41617624/1202807) shows. I've used this method myself. – Gabriel Luci Dec 25 '19 at 22:33