tags:

views:

215

answers:

4

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?

+4  A: 

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"&gt;
  <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
+1  A: 

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
+2  A: 

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  A: 

Another solution to pimp up user rights on application start is described here: http://stackoverflow.com/questions/1631444/pimp-my-uac-and-a-few-questions-about-it

Vasiliy Borovyak