|
-
Feb 6th, 2006, 04:33 PM
#1
Thread Starter
Fanatic Member
what is wrong with this code?
hello!
I have written a code that writes and reads from the registry.
to write:
Code:
// Create a new key under HKEY_LOCAL_MACHINE\Software as MCBInc
RegistryKey key = Registry.LocalMachine.OpenSubKey("Software", true);
// Add one more sub key
RegistryKey newkey = key.CreateSubKey("reg");
// Set value of sub key
newkey.SetValue("reg", toWrite);
to read:
Code:
// Create a new key under HKEY_LOCAL_MACHINE\Software as MCBInc
RegistryKey key = Registry.LocalMachine.OpenSubKey("Software", true);
// Get sub key
RegistryKey newkey = key.OpenSubKey("reg");
// Get value of sub key
return ((string)key.GetValue("reg"));
the problem is, that I can't read what I wrote! I am able to write to registry.
what is wrong?
thanks!
-
Feb 6th, 2006, 07:35 PM
#2
New Member
Re: what is wrong with this code?
// Create a new key under HKEY_LOCAL_MACHINE\Software as MCBInc
RegistryKey key = Registry.LocalMachine.OpenSubKey("Software", true);
// Get sub key
RegistryKey newkey = key.OpenSubKey("reg");
// Get value of sub key
return ((string)key.GetValue("reg"));
I think you need (string) newkey.GetValue("reg") in that last line..
I tried and it worked
-
Feb 6th, 2006, 10:28 PM
#3
Re: what is wrong with this code?
-
Feb 7th, 2006, 04:25 PM
#4
Thread Starter
Fanatic Member
Re: what is wrong with this code?
I have anothe question...
How can I know if the registry key even exists?
thanks
-
Feb 7th, 2006, 04:51 PM
#5
Re: what is wrong with this code?
The OpenSubKey method returns null if the specified key does not exist. GetValue does the same. The RegistryKey class also has GetSubKeyNames and GetValueNames methods you can use to get a String array of all subkey and value names. Let me also add that .NET apps should generally avoid using the registry to store their own data if possible.
-
Feb 8th, 2006, 04:47 PM
#6
Thread Starter
Fanatic Member
Re: what is wrong with this code?
why should they avoid using the registry?
-
Feb 8th, 2006, 05:56 PM
#7
Re: what is wrong with this code?
The registry is basically out of control. It now contains so much data, and much of it useless, that it can be a reason that computers run at less than optimum efficiency. To reduce the dependency of applications on the registry Microsoft recommends using XML as much as is practically possible for each application to store there own data locally. The application config file and serialisation are two of the tools available to you to implement this. I'm not sure exactly how things have changed in C# 2005 but VB 2005 has made the process of persisting application ridiculously simple through the My.Settings object. I'd assume that improvements have been made to config file handling in C# 2005 has improved as well, although there are many examples on the Net of how to replicate the My namespace in C# because it is such a great tool.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|