Reputation: 51
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'.
Upvotes: 4
Views: 11217
Reputation: 179
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
Upvotes: 0