Save setting to registry using C#

R

Rich P

In VB.Net I do this to save my app settings:

SaveSetting(Application.ProductName, "TimeInterval", "TimeIntKeyName",
lblTimeInterval.Text)

SaveSetting(Application.ProductName, "MyAppCurDir", "myAppKeyName",
strCurDir)

In my C# migration efforts I have encountered yet another struggle --
with the Registry. After googling/binging/checking BOL, I have not seen
anything that resemebles my sample above in C#. The samples I did find
I am not sure how to implement them as none of these samples were
straight forward like the VB.Net sample above. Instead of wrecking the
registry by experimenting I thought I would just ask -- what is the way
to perform the VB.Net operation above (setting/Saving a value to the
registry) in C#?

Thanks

Rich
 
M

Mike Lovell

Rich P said:
In VB.Net I do this to save my app settings:

SaveSetting(Application.ProductName, "TimeInterval", "TimeIntKeyName",
lblTimeInterval.Text)

SaveSetting(Application.ProductName, "MyAppCurDir", "myAppKeyName",
strCurDir)

In my C# migration efforts I have encountered yet another struggle --
with the Registry. After googling/binging/checking BOL, I have not seen
anything that resemebles my sample above in C#. The samples I did find
I am not sure how to implement them as none of these samples were
straight forward like the VB.Net sample above. Instead of wrecking the
registry by experimenting I thought I would just ask -- what is the way
to perform the VB.Net operation above (setting/Saving a value to the
registry) in C#?

Assuming the key exists, writing a string....


var companyName = "mycompany";
var productName = "Product";
var name = "name";
var value = "value";

var key =
Registry.LocalMachine.OpenSubKey(String.Format(@"SOFTWARE\{0}\{1}",
companyName, productName), true);

key.SetValue(name, value, RegistryValueKind.String);



Written outside the VS.NET environment, so check for errors!
 
R

Rich P

Thank you for your reply. I just tried
var companyName = "vbNetImageViewer";
var productName = "TimeInterval";
var name = "TimeInt";
var value = "2";

var key =
Registry.LocalMachine.OpenSubKey(String.Format(@"SOFTWARE\{0}\{1}",compa
nyName, productName), true);

key.SetValue(name, value, RegistryValueKind.String);
<<

it err'd out at

key.SetValue(name, value, RegistryValueKind.String);

err.message: Object reference not set to instance of an object

and I am not clear on the usage of var in C# yet. I tried adding a new,
but it didn't like that either. How could I set key to something?

Rich
 
M

Mike Lovell

Rich P said:
Thank you for your reply. I just tried

var companyName = "vbNetImageViewer";
var productName = "TimeInterval";
var name = "TimeInt";
var value = "2";

var key =
Registry.LocalMachine.OpenSubKey(String.Format(@"SOFTWARE\{0}\{1}",compa
nyName, productName), true);

key.SetValue(name, value, RegistryValueKind.String);
<<

it err'd out at

key.SetValue(name, value, RegistryValueKind.String);

err.message: Object reference not set to instance of an object

That's telling you that key is null, which means the registry key does not
exist:

So LocalMachine\Software\vbNetImageViewer\TimeInterval

Does not exist (according to that error). That's the assumption I said
about before the code (that it already exist)

Of course you can check for null, and create those keys if they don't exist
on the fly, then package it all up into a function, something like this:


var key = Registry.LocalMachine.OpenSubKey(String.Format(@"SOFTWARE\{0}",
companyName), true);

if (key == null) key = Registry.LocalMachine.OpenSubKey("SOFTWARE",
true).CreateSubKey(companyName);

key = key.OpenSubKey(productName);

if (key == null) key =
Registry.LocalMachine.OpenSubKey(String.Format(@"SOFTWARE\{0}",
companyName), true).CreateSubKey(productName);


Again, just written in the mail client - might be syntax errors.
and I am not clear on the usage of var in C# yet. I tried adding a new,
but it didn't like that either. How could I set key to something?

"var" is type inference. The compiled works out the type of the variable
without you having to explicitly state it.

e.g.:

var myString = "";
var myInt = 0;

Will produce a variable of type string and int respectively. In the above
case working with the registry, key is a "RegistryKey" class type.

~ Mike
 
R

Rich P

Thank you again for your reply. I did play around with your sample
suggestions and eventually got brave and tried the sameple from BOL
which ended up working. Here is what I tried (not using var)

//Delete and recreate the test key.

Registry.CurrentUser.DeleteSubKey("ImgViewerCsharp2008\\TimeInterval",
false);
RegistryKey rk =
Registry.CurrentUser.CreateSubKey("ImgViewerCsharp2008\\TimeInterval");
rk.Close();

// Obtain an instance of RegistryKey for the CurrentUser registry root.

RegistryKey rkCurrentUser = Registry.CurrentUser;

// Obtain the test key (read-only) and display it.

RegistryKey rkTest =
rkCurrentUser.OpenSubKey("ImgViewerCsharp2008\\TimeInterval",true);
rkTest.SetValue("TimeInt", "3");
Console.WriteLine("Test key: {0}", rkTest.GetValue("TimeInt"));

--and I get a 3 in the console window.

rkTest.Close();
rkCurrentUser.Close();

Now I can read/write keys. Question: if I use "var" this would seem
less verbose, but in the BOL sample they close their registrykey
objects. Any wisdom on how var deals with this? Or is this a redundant
step? Would GC close it for me (or is that only in VB.Net) ?

Rich
 
M

Mike Lovell

Rich P said:
Thank you again for your reply. I did play around with your sample
suggestions and eventually got brave and tried the sameple from BOL
which ended up working. Here is what I tried (not using var)

//Delete and recreate the test key.

Registry.CurrentUser.DeleteSubKey("ImgViewerCsharp2008\\TimeInterval",
false);
RegistryKey rk =
Registry.CurrentUser.CreateSubKey("ImgViewerCsharp2008\\TimeInterval");
rk.Close();

// Obtain an instance of RegistryKey for the CurrentUser registry root.

RegistryKey rkCurrentUser = Registry.CurrentUser;

// Obtain the test key (read-only) and display it.

RegistryKey rkTest =
rkCurrentUser.OpenSubKey("ImgViewerCsharp2008\\TimeInterval",true);
rkTest.SetValue("TimeInt", "3");
Console.WriteLine("Test key: {0}", rkTest.GetValue("TimeInt"));

--and I get a 3 in the console window.

rkTest.Close();
rkCurrentUser.Close();

Now I can read/write keys. Question: if I use "var" this would seem
less verbose, but in the BOL sample they close their registrykey
objects. Any wisdom on how var deals with this? Or is this a redundant
step? Would GC close it for me (or is that only in VB.Net) ?

Yes, you should really close them. I'm lazy and just knocked up a quick
sample. Definitely close them, good practice. Eventually the garbage
collector will do it for you, but never the less, it's a good idea.

If you used 'var' to declare the object:

var rkTest =
rkCurrentUser.OpenSubKey("ImgViewerCsharp2008\\TimeInterval",true);

The compiler would work out it was of type 'RegistryKey' for you. So it's
identical to this:

RegistryKey rkTest =
rkCurrentUser.OpenSubKey("ImgViewerCsharp2008\\TimeInterval",true);

As far as Visual Studio and the compiler are concerned. It's a matter of
preference really. It more comes into play I guess if you're using
anonymous types and classes. I always var myself, but I'm sure other people
would always declare! I think it looks a bit ugly having to do this:

MyClass myClass = new MyClass();

I like this better:

var myClass = new MyClass();

After all, I know what I'm getting back in this case anyhow!
 

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

Top