Run the .NET application as an administrator

Since Vista and Windows 7 appeared, some of my .NET applications started throwing security exceptions.

I noticed that some applications (i.e. my anti-virus, control panel) have a small screen, and when I launch these applications, administrator rights are automatically requested from me by windows.

I know that as a user I can configure the application to run as an administrator, but this is not good enough, because if the application works without privileges, it will be divided into my users.

Is there a way to tell Windows (programmatically), I want the application to run as administrator?

+5
source share
4 answers

You need to mark your application as requiring administrator privileges in the application manifest. Here is an article from MSDN magazine that explains this process.

+7
source

Create an application manifest, set requestExecutionLevel to requireAdminstrator:

Example (generated by VS when adding an application manifest):

<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
    <security>
      <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
        <!-- UAC Manifest Options
            If you want to change the Windows User Account Control level replace the 
            requestedExecutionLevel node with one of the following.

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

            If you want to utilize File and Registry Virtualization for backward 
            compatibility then delete the requestedExecutionLevel node.
        -->
        <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
      </requestedPrivileges>
    </security>
  </trustInfo>
</asmv1:assembly>

If you add this to your Visual Studio application project, it will be built into your assembly at compilation time.

+16
source

You must add the application manifest to your application and configure it to request administrator rights. See here: http://www.professionalvisualstudio.com/blog/2007/10/05/enabling-your-application-for-uac-on-vista/

+1
source

Here's another solution for suing user rights at application startup: Pimp my UAC and a few questions about it

+1
source

All Articles