1

I have an existing program, which demands for registry access using:

RegistryPermission permission = new RegistryPermission(RegistryPermissionAccess.AllAccess, strCheckPermission);
permission.Demand();

This works fine for Windows XP till Windows 7, and Windows Server 2003 and 2008. However, when I run this on Windows 8, when I demand this permission set, I still can't create a SubKey

Registry.LocalMachine.OpenSubKey("Software", true).CreateSubKey("myCompany")

Does anyone know what's going wrong in here?

Update: a stacktrace of the exception:

System.UnauthorizedAccessException: Cannot write to the registry key.
   at Microsoft.Win32.RegistryKey.EnsureWriteable()
   at Microsoft.Win32.RegistryKey.CreateSubKey(String subkey, RegistryKeyPermissionCheck permissionCheck, RegistrySecurity registrySecurity)
Peter van Kekem
  • 1,387
  • 1
  • 12
  • 30
  • Do you get an exception? If so please post full details. – Polyfun Sep 06 '12 at 10:40
  • 2
    The user must have the permission to access said registry hive. I had no problem running your code on Windows 8. – Security Hound Sep 06 '12 at 11:33
  • To confirm what @Ramhound was saying, I can run this also on Windows 8 without issue. Try running Visual Studio as Administrator and debugging – JMK Sep 06 '12 at 11:37
  • I am running VS as Administrator, and yes I do have permission to add this key via Regedit.exe, but it still doesn't write the key... – Peter van Kekem Sep 06 '12 at 11:47

1 Answers1

3

You are mixing up Code Access Security permissions with Windows rights. CAS does not do anything to give you access to registry keys, it merely verifies that the permission wasn't disabled. Which it rarely is, most .NET apps run in full trust.

This is a Window access right problem, your user account simply doesn't have the permission to write the key. Which is not unusual, HKLM keys can only ever be written with UAC elevation. You'll need to embed a manifest to ask for elevation, check this answer for details.

A further Windows 8 specific detail is WinRT, a "Windows Store" app will never be able to write such keys, no workaround for that.

Community
  • 1
  • 1
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Hi Hans, thank you for your anser, this makes sense. I've turned UAC completely off, but Windows 8 still complains sometime about UAC (or "You have to be administrator to do this", although I am administrator. I guess that is related to this issue, so I'll try to solve that first. By the way, I'm using a ClickOnce application. – Peter van Kekem Sep 06 '12 at 13:07
  • You cannot turn UAC off on Windows 8, it is used to implement sandboxing for Windows Store apps. Nor is this anything you should ever try to do, your user's machine won't have it turned off. A ClickOnce app should **never** mess with HKLM keys. – Hans Passant Sep 06 '12 at 13:31
  • Maybe I can't turn UAC completely off, but I've turned UAC to "Never notify me when...". I cannot work with UAC turned on - it's driving me crazy. – Peter van Kekem Sep 06 '12 at 14:29
  • Peter, I'm having the same problem. A .NET app that was able to create & write to a registry key when I ran it elevated in windows 7 will no longer let me in Windows 8. I finally gave in and manually did it for now. I'm trying this from the deployed app, not from Visual Studio. Have you found a solution? Thanks – Julie Lerman Nov 13 '12 at 16:10
  • same issue here. my manifest successfully requests elevated permissions. I write and read the reg value, but seems that I cannt see the settings when working in regedit. – Brady Moritz Apr 05 '13 at 20:59
  • @boom - If you don't get an exception then you have another problem. Typically it is registry redirection, getting redirected to the Wow6432Node key because your program is running in 32-bit mode on a 64-bit operating system. Change the platform target to AnyCPU or use the RegistryView enum. – Hans Passant Apr 05 '13 at 21:03
  • Ah. Im running inside visual studio, so I bet it's forcing to 32 bit. Thanks! – Brady Moritz Apr 05 '13 at 21:06