Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to request administrator permissions when the program starts?

I need my software to be able to run as administrator on Windows Vista (if someone runs it without administrative permissions, it will crash).

When launching other software, I've seen a prompt by the system like "this software will run as administrator. do you want to continue?" when the app was trying to acquire administrative privileges.

How do I request administrative privileges when running an c# app on Windows Vista?

like image 682
Arsen Zahray Avatar asked Oct 05 '11 19:10

Arsen Zahray


People also ask

How do I ask administrator permission?

Type command prompt or cmd in the search box. Then, from the search results, right-click Command Prompt and select Run as administrator. You will receive a User Account Control confirmation screen requesting you to allow the program to make changes to your computer, click Yes.

How do you get a program to stop asking for administrator?

First of all, open the shortcut properties, click the Advanced button, and see if the "Run as administrator" checkbox is unchecked.


2 Answers

Add the following to your manifest file:

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

You can also use highestAvailable for the level.

Look here about embedding manifest files:

http://msdn.microsoft.com/en-us/library/bb756929.aspx

PS: If you don't have a manifest file, you can easily add a new one:

In Visual Studio, right click project -> Add Item -> Choose Application Manifest File ( under General for Visual C# items)

The added file will already have the above part, just change the level to requireAdministrator from asInvoker

like image 135
manojlds Avatar answered Sep 30 '22 03:09

manojlds


Put this XML in a file called yourexename.exe.manifest:

<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0> <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">    <security>      <requestedPrivileges>         <requestedExecutionLevel level="highestAvailable" />      </requestedPrivileges>    </security> </trustInfo> </assembly> 
like image 34
Steve Danner Avatar answered Sep 30 '22 04:09

Steve Danner