user1914868
user1914868

Reputation: 55

Modify an existing registry key value in c#

I want to modify a data in registry path SOFTWARE\Wow6432Node\Program\SubProgram using C# code in windows 7. I am able to read the value but I can't write into Registry. Here is the code:

RegistryKey SUBKEY;
RegistryKey TAWKAY = RegistryKey.OpenRemoteBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, "");
string subkey = "SOFTWARE\\Wow6432Node\\Program\\SubProgram ";
if (TAWKAY.OpenSubKey(subkey) != null)   // Get values from Registry
{

    TAWKAY = RegistryKey.OpenRemoteBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, "");
    SUBKEY = TAWKAY.OpenSubKey(subkey); // subkey opens
    SUBKEY = TAWKAY.OpenSubKey(subkey,true); // subkey not open shows error Requested registry access is not allowed 
    SUBKEY.SetValue("Some name", "1234567890");
    Console.WriteLine(SUBKEY.GetValue("Some name").ToString());
}
else
{
    Console.WriteLine("Cannot open registry");
}

Console.Read();

If I set OpenSubKey(subkey, true), it shows an error message Requested registry access is not allowed

Is there any permission needed to write into registry? Please help me to solve the issue

Upvotes: 3

Views: 12930

Answers (3)

Xaruth
Xaruth

Reputation: 4114

Wow6432Node is not a real path in the registry. It is an alias for 32 bit keys in 64 bit OS.

You must use RegistryView.Registry32 in order to specify you want to work with 32 bits.

RegistryKey reg32key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);
RegistryKey reg_32bit_AppKey = reg32key.OpenSubKey(@"SOFTWARE\Program\SubProgram");
if (reg_32bit_AppKey != null)
{
    // Here you can work with "SOFTWARE\\Wow6432Node\\Program\\SubProgram "
}

Upvotes: 6

Suresh Jaladi
Suresh Jaladi

Reputation: 129

It is better to use "Reg" command inorder to perform any operation on registry.

Even though if you want to access the registry of remote machine you don't nedd credentials of that machine, having the machine name is sufficient.

For more information about "REG" command refer to the following link

http://technet.microsoft.com/en-us/library/cc732643(v=ws.10).aspx

Upvotes: 0

coolmine
coolmine

Reputation: 4463

Modifying/deleting/adding keys in HKLM requires administrator rights.

In that case you want to do that you will need to change your applications manifest requestedExecutionLevel value to requireAdministrator

Upvotes: 0

Related Questions