While Visual Studio Orcas and the next version of the .NET Framework
(3.5?) will include many of the new features available in Vista, most of us
today are using Visual Studio 2005 for production. You can still use all the
new features of Vista, including UAC, however, more manual steps are required
on our part.
As I stated earlier in this article, for applications in Vista to be able to use feature and functionality that require elevated privileges, the
application must inform at launch that they will be requiring those
permissions. In this scenario assume you are creating an application called
“HelloVista” as a standard .NET 2.0 Winforms application.
The first step is to create a resource file. This is done by
right-clicking on your project and selected “Add New Item…” and selecting “text
file” from the list. Rename this file ProjectName.rc where ProjectName is the
name of the Visual Studio project you are creating the resource script for.
Right click on this file and select “Open with…” and select the text editor
from the list. Past in the following code.
#define APP_MANIFEST 1
#define RT_MANIFEST 24
APP_MANIFEST RT_MANIFEST ProjectName.exe.manifest
This code will cause the ProjectName.exe.manifest as a
manifest resource in your assembly. If you are adding this manifest to an
executable, APP_MANIFEST should be set to 1. If your assembly is going to be a
DLL, then the value should be set to 2. Save this file.
The next step is to create the manifest file you just
specified in your resource script. To do this right click your project and
select “Add New Item…” and once again select “text file.” This time you are
going to name it ProjectName.exe.manifest where ProjectName is again the name
of your project. Open this file and insert the following code.
<?xml version="1.0" encoding="utf-8" ?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<requestedExecutionLevel level="requireAdministrator" />
Notice the information in the security element. We are
setting the requested execution level to “requireAdministrator” which tells Vista that the application required Administrator privileges to run. The alternative value
we could use here is “asInvoker” which allows the process to run with the same
access as the parent token, but with no access to “C:\”, “C:\Program Files” or
the HKLM node in the registry. The other available value is “highestAvailable”
which runs in the highest privileges available to the user. Save this file.
Visual Studio needs to know to compile your resource script
into a resource file. This is accomplished by adding the following pre-build step
to your projects properties.
"$(DevEnvDir)..\..\SDK\v2.0\bin\rc.exe" /r "$(ProjectDir)$(TargetName).rc"
The last step is one that might be a little scary; we need
to hand-edit the Visual Studio project file. Close Visual Studio and locate
your csproj file. Open this in note pad. Find the first <PropertyGroup>
tag and add the following line substituting the ResourceFile name with the name
of the resource script you created in the first step.
So your csproj should look something like the one shown
Save and reload your project in Visual Studio. Build your
application then navigate to the folder and execute your application. You
should be asked to confirm the applications request to run with elevated
privileges. Congratulations, you just wrote your first UAC compliant