4

I need to write and read keys in Registry without administrator mode.First I want to know is this possible to do?

Please find below code (It's working fine, when Visual studio open in Administrator mode)

Write

RegistryKey key = Registry.LocalMachine.OpenSubKey("Software", true);

key.CreateSubKey("AppName");
key = key.OpenSubKey("AppName", true);

key.CreateSubKey("AppVersion");
key = key.OpenSubKey("AppVersion", true);

key.SetValue("myval", "677");
key.Close();  

Read

RegistryKey key = Registry.LocalMachine.OpenSubKey("Software", true);

  key.CreateSubKey("AppName");
  key = key.OpenSubKey("AppName", true);
   //if it does exist, retrieve the stored values  
  if (key != null)
  {
    string s = (string)key.GetValue("myval");
    Console.Read();
    key.Close();
   }  

Please any one suggest a way to do this. (I tried to add below line as well)

Error :

ClickOnce does not support the request execution level 'requireAdministrator'.

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
saraa
  • 51
  • 1
  • 3
  • `Registry.CurrentUser` ? – ProgrammingLlama Jun 13 '18 at 12:22
  • @john I need to add key in Registry.LocalMachine – saraa Jun 13 '18 at 12:23
  • So you want to access `LocalMachine` registry keys without administrator access? – ProgrammingLlama Jun 13 '18 at 12:24
  • @john : Yes ,Exactly. – saraa Jun 13 '18 at 12:25
  • 4
    Possible duplicate of [What registry access can you get without Administrator privleges?](https://stackoverflow.com/questions/53135/what-registry-access-can-you-get-without-administrator-privleges) – ProgrammingLlama Jun 13 '18 at 12:26
  • 4
    You can't write to HKLM without admin access. You can reduce the security levels on it to allow it to be done, but that also would require admin access and in addition would make your machine vulnerable. If you need something written to HKLM, write an installer and have the installation run by a user with admin privileges, write the key during the install, and leave the protection alone. Your app can then read from HKLM when being run as a non-admin. (I'd suggest you do some searching before asking a question here. This same question has been asked (and answered) many times here before.) – Ken White Jun 13 '18 at 12:33

1 Answers1

0

Here is an answer on how to write in registry without admin

Reading is done by using the RegistryKeyPermissionCheck.ReadSubTree option

    RegistryKey baseRegistryKey = RegistryKey.OpenBaseKey(baseKey, RegistryView.Registry64);
    RegistryKey subRegistryKey = baseRegistryKey.OpenSubKey(subKey, RegistryKeyPermissionCheck.ReadSubTree);
    if (subRegistryKey != null)
    {
        object value64 = subRegistryKey.GetValue(value);
        if (value64 != null)
        {
            baseRegistryKey.Close();
            subRegistryKey.Close();
            return value64;
        }
        subRegistryKey.Close();
    }            
    baseRegistryKey.Close();

Then do the same with RegistryView.Registry32

Rubarb
  • 179
  • 1
  • 6