c sharp exe should ask for “run as administrator” prompt when opened

后端 未结 4 735
情歌与酒
情歌与酒 2021-02-09 07:20

I am having an exe generated by a c sharp program. when i run the exe, i want an UAC prompt to appear with an option to run the exe as administrator. I have seen examples of run

相关标签:
4条回答
  • 2021-02-09 07:23

    Windows vista/7/server 2008 R2 uses User Account Control (UAC) technology for security and protecting the OS from malware by limiting the privileges of an application until authorized by administrator. This is the reason why we need to make our application run as administrator. This is usually done while running the application. The process is, right click on the application and click on "Run as administrator" to run the application with administrative privileges. If suppose the user forgets to do so and runs the application normally, then unexpected behavior may be seen. Because all the actions which requires administrative privilege in win vista and above (e.g. invoking a process, using system drive, etc.) would not be performed.

    So to avoid running the application as administrator every time, we can make our application run as administrator by default. To achieve this we need some tweaking with the application manifest. An application manifest is an XML file that describes an application.

    Follow the steps to make your .net application run as administrator:

    Step 1: Go to properties of the project and click on the "View Windows Settings" button. This will open the file "app.manifest".

    Step 2: In requestedExecutionLevel key change the value of level as "requireAdministrator" and the value of uiAccess as "False". Setting level as "requireAdministrator" means the application runs only for administrators and requires that the application be launched with the full access token of an administrator. Setting uiAccess as "False" means the application does not need to drive input to the user interface of another window on the desktop. Applications that are not providing accessibility should set this flag to false. Applications that are required to drive input to other windows on the desktop (on-screen keyboard, for example) should set this value to true.

    Step 3: Save the changes, rebuild your application and install it on Win Vista or higher box.

    Now, the application would start automatically as administrator.

    0 讨论(0)
  • 2021-02-09 07:24

    You need to add "Application Manifest File". Step-By-Step

    • Right-Click on your project
    • Add "New Item"
    • Select "Application Manifest File"
    • Find this
    • change level="requireAdministrator"
    0 讨论(0)
  • 2021-02-09 07:29

    in visual studio, add an "Application Manifest File" (if you don't have one), then add <requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> in it, to request administrator privileges when running the application.

    0 讨论(0)
  • 2021-02-09 07:39

    You can use the ProcessStartInfo class or Windows principal to achieve this.

    You can also embed a manifest file in your application .exe and set requestedExecutionLevel property.

    There is also a way to enable/disable UAC trough the windows registry.

    0 讨论(0)
提交回复
热议问题