I have a tool application in WPF (.NET 4.0) wich needs to access the registry and change a subkey value. But when I try to execute the app built in x86 in a Windows Server 2008 x64, I get the error "SecurityException Requested registry access is not allowed". When I execute the same application in my Windows 8 x64, the application works perfectly.
I tried to give permission for the registry keys and even change the owner, but it's no good.
The application is running as administrator and I set this value to the manifest file:
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
</requestedPrivileges>
<applicationRequestMinimum>
<defaultAssemblyRequest permissionSetReference="Custom" />
<PermissionSet class="System.Security.PermissionSet" version="1" ID="Custom" SameSite="site" Unrestricted="true" />
</applicationRequestMinimum>
</security>
This is the method which changes the value:
localKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
localKey = localKey.OpenSubKey(RegistryHelper.Path64, RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.SetValue);
if (localKey != null)
{
localKey.SetValue(RegistryHelper.CsKey, CryptographyHelper.Encrypt(CryptographyHelper.DefaultKey, cs.ConnectionString));
localKey.SetValue(RegistryHelper.ProviderKey, provider);
localKey.Close();
}
localKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);
localKey = localKey.OpenSubKey(RegistryHelper.Path32, RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.SetValue);
if (localKey != null)
{
localKey.SetValue(RegistryHelper.CsKey, CryptographyHelper.Encrypt(CryptographyHelper.DefaultKey, cs.ConnectionString));
localKey.SetValue(RegistryHelper.ProviderKey, provider);
localKey.Close();
}
When I change the build to AnyCPU, the application changes the value as expected on WinServer 2008 x64, but not when built as x86. In my Windows 8 x64 it works in both x86 and x64 perfectly. Do you guys have any clues?