• Advertisement
Sign in to follow this  

C# Invisible Registry Value?

This topic is 4523 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've never touched C# before but I've got this program I made and I thought I'd take a shot at converting it to C# as an attempt to start learning the language a bit but I've come across a strange problem. I thought I'd use the Registry/RegistryKey classes to store some program settings in the registry but I was having trouble with it so I wrote a quick test program that from what I can make out so far, should do what I want.
using Microsoft.Win32;

public class RegAccess
{
	//Main function
	public static void Main()
	{
		//Registry key, initialised to HKEY_LOCAL_MACHINE
		RegistryKey myKey = Registry.LocalMachine;
		object myData;

		//Attempt to open the subkey
		if(myKey.OpenSubKey("Software\\NBSoft\\") != null)
		{
			//Subkey opened, read in values
			myData = myKey.GetValue("Test");
			System.Console.WriteLine(myData.ToString());

			//Close the key
			myKey.Close();
		}
		else
		{
			//Create the subkey
			myKey.CreateSubKey("Software\\NBSoft\\");

			//Open the subkey
			if(myKey.OpenSubKey("Software\\NBSoft\\", true) != null)
			{
				//Write out the test value
				myKey.SetValue("Test", "Test Value! Wahey!");

				//Close the key
				myKey.Flush();
				myKey.Close();
			}
		}
	}
}






I ran this program twice and got the expected console message "Test Value! Wahey!" but if I launch regedit and goto HKLM\\Software the "NBSoft" key is there but when I click it no values appear. I can still run the program and get the message so apparantly the program is reading the value, but where from if regedit can't seem to find it? Edit: I have also done a search in regedit for the value with no luck :- Any ideas anyone? Thanks a lot!

Share this post


Link to post
Share on other sites
Advertisement
Looks like you're writing the value directly to HKEY_LOCAL_MACHINE. RegistryKey.OpenSubKey returns a new RegistryKey object that you use to perform operations on that key. Furthermore, OpenSubKey will create keys as needed, so you can simplify your code to:


using Microsoft.Win32;

public class RegAccess
{
//Main function
public static void Main()
{
//Registry key, initialised to HKEY_LOCAL_MACHINE
RegistryKey myKey = Registry.LocalMachine.OpenSubKey("Software\\NBSoft");
object myData = myKey.GetValue("Test");

if (myData == null)
{
// value doesn't exist, write it
myKey.SetValue("Test", "Test Value! Wahey!");
}
else
{
// read and display the value
System.Console.WriteLine(myData.ToString());
}

myKey.Close();
}
}


Share this post


Link to post
Share on other sites
Thanks a lot for the help!

Your code caused an exception on the line "object myData = myKey.GetValue("Test");" because the call to Registry.LocalMachine.OpenSubKey() didn't create the registry key. I just took out the myKey.GetValue() part and first checked if myKey was null or not before creating the key or reading from it.

Thanks again for the help, I have the program working now and I can see the value in regedit. I had a look for the value I wrote to the registry before in HKLM but it isn't there so I guess now my registry is stuck with a ghost value :S

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement