« PreviousNext »

How to run your C# Application as Administrator in Windows Vista?

26 August 2007

To have your C# (or any .NET program) run as Administrator in Windows, you'll have to create a manifest for it. What is a manifest file? I think Microsoft explains it best:

Every assembly, whether static or dynamic, contains a collection of data that describes how the elements in the assembly relate to each other. The assembly manifest contains this assembly metadata. An assembly manifest contains all the metadata needed to specify the assembly's version requirements and security identity, and all metadata needed to define the scope of the assembly and resolve references to resources and classes. The assembly manifest can be stored in either a PE file (an .exe or .dll) with Microsoft intermediate language (MSIL) code or in a standalone PE file that contains only assembly manifest information. (link to the entire MSDN article)

With a Manifest you're able to tell Windows Vista that your C# program wants to run as Administrator. This will cause the Vista confirmation window to pop up asking the user to grant the program access. Running as Administrator in Vista is required, for example, if your program is trying to create a WCF endpoint.

The following manifest XML tells the .NET Framework to run the Assembly that you specify as Administrator within Windows Vista:

PLAIN TEXT
XML:
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
  3. <assemblyIdentity version="1.0.0.0"
  4. processorArchitecture="X86"
  5. name="someExecName"
  6. type="win32" />
  7. <description>Your Program Description</description>
  8. <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3"><security>
  9. <requestedPrivileges>
  10. <requestedExecutionLevel level="requireAdministrator" />
  11. </requestedPrivileges>
  12. </security>
  13. </trustInfo></assembly>

Now, there are two ways to make your program use this Manifest:

  1. Rename it to (YourEXEName).manifest. The .NET Framework when executing the file will see the Manifest and handle its contents.
  2. Embed the .manifest file into you EXE. This can be done by executing the following command line:

Now your own C# application will prompt to run as Administrator in Windows Vista!

Cheers!

Posted in C# Programming, General Programming, General Software, Windows Vista | Trackback | del.icio.us | Top Of Page

    2 Responses to “How to run your C# Application as Administrator in Windows Vista?”

  1. Krunal Says:

    Can we disable Windows Vista confirmation popup and set it programatically?

    Also I want to run my C# application as administrator only once and then after I want it to run as normal user.

  2. Sujith Says:

    Thanks for your post, its very much useful.

Leave a Reply


Powered by WP Hashcash