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?
 
Add

using System.Security.Permissions;

to the build and try it again.
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top