From: Rich P on 10 Mar 2010 15:39 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 *** Sent via Developersdex http://www.developersdex.com ***
From: Mike Lovell on 10 Mar 2010 15:54 "Rich P" <rpng123(a)aol.com> wrote in message news:unPYJHJwKHA.5036(a)TK2MSFTNGP02.phx.gbl... > 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! -- Mike GoTinker, C# Blog http://www.gotinker.com
From: Rich P on 10 Mar 2010 16:19 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 *** Sent via Developersdex http://www.developersdex.com ***
From: Mike Lovell on 10 Mar 2010 16:47 "Rich P" <rpng123(a)aol.com> wrote in message news:elYLXdJwKHA.2436(a)TK2MSFTNGP04.phx.gbl... > 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
From: Rich P on 10 Mar 2010 17:17 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 *** Sent via Developersdex http://www.developersdex.com ***
|
Next
|
Last
Pages: 1 2 Prev: regular expression(Regex) Next: Granting admin priveleges to a process started from a C# app |