{"id":211,"date":"2009-03-11T18:49:00","date_gmt":"2009-03-11T18:49:00","guid":{"rendered":"http:\/\/windowsvj.com\/wpblog\/?p=211"},"modified":"2009-03-11T18:49:00","modified_gmt":"2009-03-11T18:49:00","slug":"working-with-windows-registry-using-vbnet","status":"publish","type":"post","link":"http:\/\/windowsvj.com\/wpblog\/2009\/03\/working-with-windows-registry-using-vbnet\/","title":{"rendered":"Working with Windows Registry using VB.NET"},"content":{"rendered":"<h5>Creating a Subkey<\/h5>\n<div class=\"SmallText\" id=\"premain0\" style=\"width: 100%; cursor: pointer;\"><img loading=\"lazy\" decoding=\"async\" preid=\"0\" src=\"file:\/\/\/E:\/Appz%20Dev\/WinRegHkr%20%5BVJ%5D\/registry_with_vb.aspx_files\/minus.gif\" id=\"preimg0\" width=\"9\" height=\"9\" \/><span preid=\"0\" style=\"margin-bottom: 0pt;\" id=\"precollapse0\"> Collapse<\/span><\/div>\n<div class=\"SmallText\" id=\"premain0\" style=\"width: 100%; cursor: pointer;\"><img loading=\"lazy\" decoding=\"async\" preid=\"0\" src=\"file:\/\/\/images\/minus.gif\" id=\"preimg0\" width=\"9\" height=\"9\" \/><span preid=\"0\" style=\"margin-bottom: 0pt;\" id=\"precollapse0\"> Collapse<\/span><\/div>\n<pre style=\"margin-top: 0pt;\" id=\"pre0\">Dim regKey As RegistryKey<br \/>regKey = Registry.LocalMachine.OpenSubKey(<span class=\"code-string\">\"<\/span><span class=\"code-string\">SOFTWARE\"<\/span>, True)<br \/>regKey.CreateSubKey(<span class=\"code-string\">\"<\/span><span class=\"code-string\">MyApp\"<\/span>)<br \/>regKey.Close()<br \/>       <\/pre>\n<p>In the code snippet shown above, I have created a subkey under HKLM\\Software called MyApp. Note that I passed True as the second parameter to the OpenSubKey method. This boolean value is to indicate whether the key is writable or not. For instance, you can set it to false if you are just reading data from the registry. <\/p>\n<h5>Reading and writing values<\/h5>\n<div class=\"SmallText\" id=\"premain1\" style=\"width: 100%; cursor: pointer;\"><img loading=\"lazy\" decoding=\"async\" preid=\"1\" src=\"file:\/\/\/E:\/Appz%20Dev\/WinRegHkr%20%5BVJ%5D\/registry_with_vb.aspx_files\/minus.gif\" id=\"preimg1\" width=\"9\" height=\"9\" \/><span preid=\"1\" style=\"margin-bottom: 0pt;\" id=\"precollapse1\"> Collapse<\/span><\/div>\n<div class=\"SmallText\" id=\"premain1\" style=\"width: 100%; cursor: pointer;\"><img loading=\"lazy\" decoding=\"async\" preid=\"1\" src=\"file:\/\/\/images\/minus.gif\" id=\"preimg1\" width=\"9\" height=\"9\" \/><span preid=\"1\" style=\"margin-bottom: 0pt;\" id=\"precollapse1\"> Collapse<\/span><\/div>\n<pre style=\"margin-top: 0pt;\" id=\"pre1\">Dim regKey As RegistryKey<br \/>Dim ver As Decimal<br \/>regKey = Registry.LocalMachine.OpenSubKey(<span class=\"code-string\">\"<\/span><span class=\"code-string\">Software\\MyApp\"<\/span>, True)<br \/>regKey.SetValue(<span class=\"code-string\">\"<\/span><span class=\"code-string\">AppName\"<\/span>, <span class=\"code-string\">\"<\/span><span class=\"code-string\">MyRegApp\"<\/span>)<br \/>ver = regKey.GetValue(<span class=\"code-string\">\"<\/span><span class=\"code-string\">Version\"<\/span>, <span class=\"code-digit\">0<\/span>.<span class=\"code-digit\">0<\/span>)<br \/>If ver <span class=\"code-keyword\"><<\/span>  <span class=\"code-digit\">1<\/span>.<span class=\"code-digit\">1<\/span> Then<br \/>regKey.SetValue(<span class=\"code-string\">\"<\/span><span class=\"code-string\">Version\"<\/span>, <span class=\"code-digit\">1<\/span>.<span class=\"code-digit\">1<\/span>)<br \/>End If<br \/>regKey.Close()<br \/><\/pre>\n<p>In the code snippet shown above, I am creating two values AppName and Version. I am also setting the values to MyRegApp and 1.1 respectively. Note: If you recollect that in the previous sections I had mentioned about data types for registry values like REG_SZ. But nowhere in the above code we mentioned about the data type. This is because .NET runtime interprets the type itself based on what is passed as value and we do not need to pass it explicitly. <\/p>\n<h5>Deleting a Subkey<\/h5>\n<div class=\"SmallText\" id=\"premain2\" style=\"width: 100%; cursor: pointer;\"><img loading=\"lazy\" decoding=\"async\" preid=\"2\" src=\"file:\/\/\/E:\/Appz%20Dev\/WinRegHkr%20%5BVJ%5D\/registry_with_vb.aspx_files\/minus.gif\" id=\"preimg2\" width=\"9\" height=\"9\" \/><span preid=\"2\" style=\"margin-bottom: 0pt;\" id=\"precollapse2\"> Collapse<\/span><\/div>\n<div class=\"SmallText\" id=\"premain2\" style=\"width: 100%; cursor: pointer;\"><img loading=\"lazy\" decoding=\"async\" preid=\"2\" src=\"file:\/\/\/images\/minus.gif\" id=\"preimg2\" width=\"9\" height=\"9\" \/><span preid=\"2\" style=\"margin-bottom: 0pt;\" id=\"precollapse2\"> Collapse<\/span><\/div>\n<pre style=\"margin-top: 0pt;\" id=\"pre2\">Dim regKey As RegistryKey<br \/>regKey = Registry.LocalMachine.OpenSubKey(<span class=\"code-string\">\"<\/span><span class=\"code-string\">Software\"<\/span>, True)<br \/>regKey.DeleteSubKey(<span class=\"code-string\">\"<\/span><span class=\"code-string\">MyApp\"<\/span>, True)<br \/>regKey.Close()<br \/>       <\/pre>\n<p>In the code snippet shown above, I am deleting the subkey MyApp and all its values. Note that in the call to DeleteSubkey, I have passed a second Boolean argument of True. This means that an exception is thrown when the key to be deleted is not found <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Creating a Subkey Collapse Collapse Dim regKey As RegistryKeyregKey = Registry.LocalMachine.OpenSubKey(&#8220;SOFTWARE&#8221;, True)regKey.CreateSubKey(&#8220;MyApp&#8221;)regKey.Close() In the code snippet shown above, I have created a subkey under HKLM\\Software called MyApp. Note that I passed True as the second parameter to the OpenSubKey method. This boolean value is to indicate whether the key is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-211","post","type-post","status-publish","format-standard","hentry","category-net"],"_links":{"self":[{"href":"http:\/\/windowsvj.com\/wpblog\/wp-json\/wp\/v2\/posts\/211","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/windowsvj.com\/wpblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/windowsvj.com\/wpblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/windowsvj.com\/wpblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/windowsvj.com\/wpblog\/wp-json\/wp\/v2\/comments?post=211"}],"version-history":[{"count":0,"href":"http:\/\/windowsvj.com\/wpblog\/wp-json\/wp\/v2\/posts\/211\/revisions"}],"wp:attachment":[{"href":"http:\/\/windowsvj.com\/wpblog\/wp-json\/wp\/v2\/media?parent=211"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/windowsvj.com\/wpblog\/wp-json\/wp\/v2\/categories?post=211"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/windowsvj.com\/wpblog\/wp-json\/wp\/v2\/tags?post=211"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}