5

Since Vista & windows 7 came out some of my .NET application has started throwing security exceptions.

I've noticed that some applications (i.e. my antivirus, control panel) have a small shield and when I run these applications administrator privileges are automatically requested from me by windows.

I know that as a user I can set the application to run as administrator but that's not good enough because if the application will run without privileges it would crash on my users machines.

Is there a way to tell windows (programmatically) I want the application to run with administrative privileges?

Dror Helper
  • 30,292
  • 15
  • 80
  • 129

4 Answers4

16

Create an application manifest, set the requestedExecutionLevel to requireAdminstrator:

Example (generated by VS when you add 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 a Visual Studio application project, it will be embedded into your assembly when you compile.

dso
  • 9,463
  • 10
  • 53
  • 59
7

You need to mark your app as requiring admin privileges in the application manifest. Here's an article from MSDN Magazine that explains the process.

Gene Goykhman
  • 1,981
  • 2
  • 16
  • 15
1

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

Konamiman
  • 49,681
  • 17
  • 108
  • 138
1

Another solution to pimp up user rights on application start is described here: Pimp my UAC and a few questions about it

Community
  • 1
  • 1
Vasyl Boroviak
  • 5,959
  • 5
  • 51
  • 70