1

I have a issue reading a registry value programmatically using C#. I looked into many sites and help but could not find any helpful. I am able to access and read registry when I run VS in eleveated mode, but face issue when I run VS with out elevated mode. Initially I started with the below code

byte[] val = (byte[])Registry.GetValue("HKEY_LOCAL_MACHINE\\Software\\MyServices\\Identity\\ASPNET_SETREG", "ValueName", 0);

This worked fine with elevated mode, but fails in non elevated mode. Placed the attribute on top of the function

[RegistryPermissionAttribute(SecurityAction.Demand,Unrestricted=true)]

This did not help. Then Tried

[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.LinkDemand, Flags = System.Security.Permissions.SecurityPermissionFlag.AllFlags)] 

Still did not work. Now I Tried the below code...

RegistryKey key = Registry.LocalMachine;            


        RegistrySecurity rs = new RegistrySecurity();
        rs = key.GetAccessControl();
        string user = "DomainName\\Username";
        rs.AddAccessRule(new RegistryAccessRule(user,
        RegistryRights.ReadKey,
        InheritanceFlags.None,
        PropagationFlags.None,
        AccessControlType.Allow));


        key.SetAccessControl(rs);//Exception: "Attempted to perform an unauthorized operation."}
        //RegistryKey key2 = key.OpenSubKey("Software\\MyServices\\Identity\\ASPNET_SETREG");
        //RegistryKey key2 = key.OpenSubKey("Software\\MyServices\\Identity\\ASPNET_SETREG", false);
        //RegistryKey key2 = key.OpenSubKey("Software\\MyServices\\Identity\\ASPNET_SETREG", RegistryKeyPermissionCheck.ReadSubTree);
        RegistryKey key2 = key.OpenSubKey("Software\\MyServices\\Identity\\ASPNET_SETREG", RegistryKeyPermissionCheck.ReadSubTree, RegistryRights.ReadPermissions);

Commenting SetAccessControl and use any of the OpenSubkey option, I get Exception: "Requested registry access is not allowed."

I am badly stuckup and unable to proceed.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Murthy
  • 177
  • 2
  • 12
  • Try navigating to the registry key in question using `regedit`; right-click on the key and select the _Permissions_ option. – LiquidPony Feb 17 '12 at 16:31
  • Forgot to mention, I tried doing that earlier! – Murthy Feb 17 '12 at 16:35
  • Strangely, I see that the Creater Owner does not have full control or read permission, but the System, Administrator and users have full control and read permissions. The creator owner has only special permissions. So clicked on advanced... In the permissions tab, I see for Creator owner, the permission applies to subkeys only, but for others the permission applies to this key and subkeys! this surprises me! – Murthy Feb 17 '12 at 16:40
  • If you have UAC on you get prompted anyway, lowering teh security on the item and possible it's owners would be a tad foolish. Basically to access this key you need to run as admin is what it's saying. One of the main points of UAC was to block programatic elevation as it was a huge threat vector, short of turning it off, you are stuffed I think. – Tony Hopkinson Feb 17 '12 at 16:45
  • :( This scares me! I wont be able to proceed without doing this! If this is not at all possible, I need to think of storing my data in a file instead of registry! – Murthy Feb 17 '12 at 16:51

2 Answers2

1

The windows registry is basically a structured file system, and has permissions for keys and values.

You do not have the permissions set correctly on ...\MyServices\ or deeper keys - you have no permission to access those from your unprivileged process.

Either:

  1. Those keys should be readable by anybody, so you should change the permissions to make them readable by everybody. Or -
  2. Those keys were intentionally restricted for a good reason, and so they should not be readable by everybody, in which case your program should always run elevated.
antiduh
  • 11,853
  • 4
  • 43
  • 66
1
private RegistryKey keyR = Registry.CurrentUser.OpenSubKey("Software\\YourKey",true);
private RegistryKey keyW = Registry.CurrentUser.CreateSubKey("Software\\YourKey");

public string version
{
    get { return keyR.GetValue("VERSION", "", RegistryValueOptions.DoNotExpandEnvironmentNames).ToString(); }
    set { keyW.SetValue("VERSION", value, RegistryValueKind.String); }
}

I am using windows registry in this way. No problem...

Alex
  • 897
  • 10
  • 17
  • The moment it hits, OpenSubkey, I get the exception! – Murthy Feb 17 '12 at 16:36
  • try this way `Registry.CurrentUser.OpenSubKey("Software\\YourKey",true);` – Alex Feb 20 '12 at 09:50
  • You guys are off target a little bit - the problem isn't with accessing the registry in general - both of your methods work just fine - the problem is that the registry is just a file system, and like any other file system, restricts access to certain parts. – antiduh Dec 28 '13 at 16:42
  • In this case the application should require administrator rights. Ex:[link](http://stackoverflow.com/questions/17406751/wpf-application-manifest-file) – Alex Jan 06 '14 at 15:55