Hi,
I'm developing a C# .Net 2.0 application that requires read/write access to the registry. I'm using the following code:
using Microsoft.Win32;
...
regkey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE", true);
regkey = regkey.CreateSubKey("MyCompany");
On Vista the new permissions lock down, I get the registry access denied exception.
How do I elevate the permissions level so that the user at least is prompted to allow access
Thanks,
Mike

Registry Access in Managed Form Application
KasperDk
Well I've worked out that I need a manifest file to let Vista know that the application requires elevated permissions.
However when I run the application on Vista nothing happens (no popup, nothing). If I rename the manifest file it launchers fine (with the permissions error).
My Manifest looks a little like this...
< xml version="1.0" encoding="UTF-8" standalone="yes" >
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity version="1.0.2335.19038"
processorArchitecture="X86"
name="myApp"
type="win32"/>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel
level="requireAdministrator"
uiAccess="true"/>
</requestedPrivileges>
</security>
</trustInfo>
</assembly>
Any help would be appriecated.
Mike
Dave Lubash - MSFT
Ok we have a winner... it seems embedding the manifest as a resource does work, it just doesn't work when running in debug mode. Maybe this is a beta bug
For the benefit of anyone else trying to figure this out, here's how...
1) Create a resource script in notepad as follows, note the 1 which assumes an application, call this manifest.rc.
#include <winuser.h>
#define IDR_MANIFEST 1 // 2 for a DLL, 1 for an app
IDR_MANIFEST RT_MANIFEST MOVEABLE PURE
{
"<assembly xmlns=""urn:schemas-microsoft-com:asm.v1"" manifestVersion=""1.0"">
<asmv3:trustInfo xmlns:asmv3=""urn:schemas-microsoft-com:asm.v3"">
<asmv3:security>
<asmv3:requestedPrivileges>
<asmv3:requestedExecutionLevel
level=""requireAdministrator""
uiAccess=""false"" />
</asmv3:requestedPrivileges>
</asmv3:security>
</asmv3:trustInfo>
</assembly>"
}
2) Run the following command line to complie this into a resource file (manifest.res). You will need the Windows SDK installed.
rc.exe manifest.rc
3) In Visual Studio right click on the project and select properties. On the application tab check the resource file checkbox and select the manifest.res file created in step 2.
4) Complie the solution, then find the output and execute manually. Vista will prompt to elevate permissions to administrator level and everything should work!
Oddly adding a manifest to a referrenced dll (which does the registry work) , and leaving this out of the main application did not work. I found I just needed to add the manifest to the application itself and everything else fell into place.
Dionne
Tianung
It seems in simplifying my example I've written one that does actually work fine... opps.
Just to be clear in case someone has an answer, I'm trying to adjust the policy settings in the registry and it's this that fails with permissions.
RegistryKey regkey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Policies", true);regkey = regkey.CreateSubKey("System");
Still looking for a way to elevate the permissions so that the application can run as admin. I've now tried an embedded manifest file as a resource, but that doesn't seem to work either.
aservin
Hi Mike,
I tries your solution and it wokred for me. Thanks. But... it woked until I signed the assemly. I tried adding publickKeyToken to asseblyIdentity node, but in vain. Have you come across this issue