EToS
{ ADMIN }
posts: 11
last: 02-Aug-2008
TITLE: Running Applications As Administrator under Windows Vista
DESCRIPTION: How to make a C-Sharp application request administrative priviliedges under vista
Submitted: 13-Jun-2008 04:44:15 ( 12w 3d 6h ago ) Language: .NET (*.)
Views: 179 Lines of Code: 42 LINES
Rating:
rate: star1
star2
star3
star4
star5
dstar1
dstar2
dstar3
dstar4
dstar5  ( rated! )
  { 5.00 / 5 }
Difficulty: Intermediate
Bookmark
Introduction

Running as administrator under vista

In order to have your application running as Administrator under Vista you will need to create a .manifest file. Microsoft describes manifest files as:

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)

By creating a manifest file for your C# (or any .NET) application, you can tell Windows Vista that you need your application to run under Administrator priviledges. In Vista this will bring up a confirmation dialog requiring a user to agree to the programs access. Running as Administrator in Vista is required, for example, if your program is trying to create a WCF endpoint.

Solution

how to make a manifest file

manifest files are formatted in XML, the following will tell the .NET Framework to run the Assembly that you specify as Administrator within Windows Vista:

<?xml version="1.0" encoding="utf-8" ?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity version="1.0.0.0"
processorArchitecture="X86"
name="APP-NAME.exe"
type="win32" />
<description>Your Program Description</description>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3"><security>
<requestedPrivileges>
<requestedExecutionLevel level="requireAdministrator" />
</requestedPrivileges>
</security>
</trustInfo></assembly>

There are 2 ways to your Application use the above manifest file:

  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:

    • mt -manifest YourProgram.exe.manifest -outputresource:YourProgram.exe

    • If your assembly is strong named, you will be unable to embed the manifest into it as it would invalidate the strong naming.

 

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