[2.0] Registry, need help.
Ok well I got my program reading from registry using
C# Code:
Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.Users;
textBox1.Text = rk.GetValue("vBull cookie").ToString();
now it errors, "Object reference not set to an instance of an object." im guessing that is because the registry doesn't exist. Is there a way to check for a registry and set it? or is there something else wrong?
Re: [2.0] Registry, need help.
Does the Users hive of your Windows registry have a value named "vBull cookie" at the root level? I very much doubt it. I would guess that if there is a value with that name it is under some subkey. In that case you need to call the OpenSubKey method of the Users hive to get to that subkey, then call ITS GetValue method.
Also, does this value contain a string? If so then why call its ToString method? You don't have to convert a string to a string. GetValue returns an Object reference but if the object referred to is a string then you should be casting it as that type. If you do that then your code won't throw any exception even if the value doesn't exist. It will simply return a null reference of type string:
C# Code:
Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.Users.OpenSubKey("subkey path here");
textBox1.Text = rk.GetValue("vBull cookie") as string;
// or
// textBox1.Text = (string)rk.GetValue("vBull cookie");
The advantage of using the 'as' keyword is that it will return a null reference even if the actual object is not the type you're casting as, while the explicit cast requires that the actual object be that type.
Re: [2.0] Registry, need help.
Quote:
Originally Posted by jmcilhinney
Does the Users hive of your Windows registry have a value named "vBull cookie" at the root level? I very much doubt it. I would guess that if there is a value with that name it is under some subkey. In that case you need to call the OpenSubKey method of the Users hive to get to that subkey, then call ITS GetValue method.
You confused me there. Can you make a sample function that will check for a registry(like the one i'm getting a string from) and if its not there make it. thanks.
Re: [2.0] Registry, need help.
It's not "a registry". "The registry" is the whole Windows registry database. The registry contains keys and values. If you view the registry in a registry editor like regedit supplied with Windows it is displayed much like the file system is in Windows Explorer. The keys are like folders and the values are like files. If you think of the registry properly, in terms of keys and value, then it's quite simple to use it.
The Registry class has a property of type RegistryKey for each of the main hives: LocalMachine, CurrentUser, etc. Each of those properties is a RegistryKey. Read the documentation for the RegistryKey class and its member listing.
If you want to access a value you have to first open the key it belongs to. That key will be a subkey of one of the main hives. That means that you need to use OpenSubKey method of that appropriate property of the Registry class, as I did in my previous post. That will return a RegistryKey object for the subkey you just opened. Now you have the key you can access its values. Now that you've read the documentation for the RegistryKey class you know how to use the GetValue, SetValue, etc. methods. Make sure that if you want to make any changes to the key, i.e. setting or deleting values, that you open it with write access. Having read the documentation for the OpenSubKey method you know how to do that.