How do I read the registry in C#?

  • Thread starter Thread starter Guest
  • Start date Start date
When I try and save I get an error saying can not write to the registry key
for example

RegistryKey key =
Registry.CurrentUser.OpenSubKey(@"Software\\Company\\Folder\\Settings" );
key.CreateSubKey(@"Database");
key.SetValue(@"Database", "c:\abc.mdb" );

Do I have to do something with permissions?
 
Hard to say without knowing what error you get, but your code snippet
shows some confusion as how to use verbatim string literals (@"string").

In verbatim string literals you don't have to, and should not, escape
for instance backslashes. When using regular string literals you should.
You seem to have the two mixed up. Try changing the code to:

RegistryKey key =
Registry.CurrentUser.OpenSubKey(@"Software\Company\Folder\Settings" );
key.CreateSubKey(@"Database");
key.SetValue(@"Database", @"c:\abc.mdb" );

If that doesn't help, you should provide the error you get.

Regards,
Joakim
 
OpenSubKey(string Name, bool writable)
Pass true for second parameter to open writable keys
-Praveen
 
Back
Top