{"id":104,"date":"2009-09-30T22:13:26","date_gmt":"2009-09-30T22:13:26","guid":{"rendered":"http:\/\/dirk.forbiddendream.be\/?p=104"},"modified":"2016-01-07T10:50:19","modified_gmt":"2016-01-07T10:50:19","slug":"working-with-windows-registry-part-2-writing-to-the-registry","status":"publish","type":"post","link":"https:\/\/dirk.schuermans.me\/?p=104","title":{"rendered":"Working with Windows Registry in VB.NET \u2013 Part 2: Writing to the Registry"},"content":{"rendered":"<p>Welcome to the second part!<\/p>\n<p>In this part I&#8217;ll teach you how to write stuff away inside the Windows Registry.<\/p>\n<p><!--more--><br \/>\n<a href=\"#summary\">Proceed to the summary of this post<\/a><\/p>\n<p><span style=\"color: #ff0000;\"><strong>*NOTE: For the remainder of this 5-part blog entry, i&#8217;ll be using the HKEY_LOCAL_MACHINE hive to read and write from the registry.<\/strong><\/span><\/p>\n<p>Ok, let&#8217;s say we got an application called &#8220;MyApp&#8221; and we have certain settings we would like to store inside the Windows Registry.<br \/>\nSettings such as the <span style=\"text-decoration: underline;\"><strong>version<\/strong><\/span> of the program, the <span style=\"text-decoration: underline;\"><strong>author<\/strong><\/span> (programmer) and a <span style=\"text-decoration: underline;\"><strong>serial<\/strong><\/span> for the program.<\/p>\n<p>All software settings are stored within the <strong>SOFTWARE<\/strong> subkey from our used hive, so it&#8217;s obvious that our settings should be stored within <strong>HKEY_LOCAL_MACHINE\\SOFTWARE\\MyApp<\/strong><\/p>\n<p><em><span style=\"color: #ff0000;\">A special note regarding this:<\/span><\/em><\/p>\n<p><em><span style=\"color: #ff0000;\">Always keep in mind the following:<br \/>\nHKEY_LOCAL_MACHINE\\SOFTWARE\\&#8230; for 64-bit applications on a 64-bit operating system OR for 32-bit applications on a 32-bit operating system<br \/>\nHKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\&#8230; for 32-bit applications on a 64-bit operating system<\/span><\/em><\/p>\n<p><em><span style=\"color: #ff0000;\">Code-wise this isn&#8217;t really an issue, since it&#8217;ll automaticly store data within the correct subkey. Only if you plan to manually lookup and\/or change settings during the development<br \/>\nof your program you&#8217;ll have to look under the correct subkey. I don&#8217;t want you freaking out like &#8220;Omg, it stored the settings somewhere hidden in my Registry and now I can&#8217;t find it!&#8221;<\/span><\/em><\/p>\n<p><em><span style=\"color: #ff0000;\">I myself am using a 64-bit operating system. Keep that in mind for the remainder of this post.<\/span><\/em><\/p>\n<p>Ok, now that we have that cleared out, let&#8217;s continue shall we? \ud83d\ude42<\/p>\n<p>The .NET framework provides us with 2 base classes located within the <strong>Microsoft.Win32 <\/strong>namespace to interact with the Registry.<\/p>\n<p>&#8211; <strong>Microsoft.Win32.Registry<\/strong><br \/>\n&#8211; <strong>Microsoft.Win32.RegistryKey<\/strong><\/p>\n<p>So you might want to start off with using the Microsoft.Win32 namespace in your project:<\/p>\n<pre name=\"code\" class=\"vb.net\">'Importing the Microsoft.Win32 Namespace\r\nImports Microsoft.Win32<\/pre>\n<p>Once you&#8217;ve done that, it&#8217;s time to work with those 2 classes.<br \/>\nWe&#8217;ll start off by declaring a variable called oRegKey as a RegistryKey:<\/p>\n<pre name=\"code\" class=\"vb.net\">'Declaring our Registry key\r\nDim oRegKey As RegistryKey<\/pre>\n<p>After that we define where the key has to work:<\/p>\n<pre name=\"code\" class=\"vb.net\">'Define the subkey we're going to use in writeable mode\r\noRegKey = Registry.LocalMachine.OpenSubKey(\"SOFTWARE\", True)<\/pre>\n<p><span style=\"color: #ff0000;\">Note: the &#8220;true&#8221; defines wether we can write to the subkey or not.<\/span><br \/>\nAlright, so we&#8217;re working inside &#8220;HKEY_LOCAL_MACHINE\\SOFTWARE&#8221;<\/p>\n<p><strong>A good habbit is to check wether the subkey you plan on using (in our case MyApp) already exists or not<\/strong>. I assume you&#8217;re more then capable of writing that small code snippet yourself.<br \/>\nOnce we&#8217;re sure that the subkey doesn&#8217;t exist, let&#8217;s create it!<\/p>\n<p>To create a subkey, use the following code snippet:<\/p>\n<pre name=\"code\" class=\"vb.net\">'Create a SubKey for our program\r\noRegKey.CreateSubKey(\"MyApp\")<\/pre>\n<p>Now, in order to store our settings under that subkey, we have to say that our oRegKey has to be working inside that subkey:<\/p>\n<pre name=\"code\" class=\"vb.net\">'Define our own SubKey to work with in writeable mode\r\noRegKey = Registry.LocalMachine.OpenSubKey(\"SOFTWARE\\MyApp\", True)<\/pre>\n<p>Once we&#8217;ve done that, we can start storing our settings!<\/p>\n<pre name=\"code\" class=\"vb.net\">'RegistryKey.SetValue example\r\noRegKey.SetValue(Name As String, Value As Object)<\/pre>\n<p>So in our case:<\/p>\n<pre name=\"code\" class=\"vb.net\">'Store values\r\noRegKey.SetValue(\"Version\", \"1.0\")\r\noRegKey.SetValue(\"Author\", \"Dirk Schuermans\")\r\noRegKey.SetValue(\"Serial\", \"A1B2C3D4E5F6G7H8I9\")<\/pre>\n<p>Once you&#8217;ve run those commands, you can find those settings stored in your Registry!<\/p>\n<p>HKEY_LOCAL_MACHINE\\SOFTWARE\\MyApp\\&#8230; for 64-bit applications on a 64-bit Operating System or for 32-bit applications on a 32-bit Operating System<br \/>\nHKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\MyApp\\&#8230; for 32-bit applications on a 64-bit Operating System<\/p>\n<p>Finally we&#8217;ll close the regkey with the following command:<\/p>\n<pre name=\"code\" class=\"vb.net\">'Close the RegistryKey\r\noRegKey.Close()<\/pre>\n<p>There we go! You know posses the knowledge of how to write to the Windows Registry!<br \/>\nPlease keep in mind that different parts of the Registry require different security permissions. The 2 software subkeys are usually writeable by regular users aswell.<\/p>\n<p><a name=\"summary\"><\/a><br \/>\n<span style=\"color: #ff0000;\"><span style=\"text-decoration: underline;\"><strong>POST SUMMARY<\/strong><\/span><\/span><\/p>\n<pre name=\"code\" class=\"vb.net\">'Import correct namespace\r\nImports Microsoft.Win32\r\n\r\n'Procedure to store stuff in the Registry\r\nPublic Sub fnWrite()\r\n\t'Declare variables\r\n\tDim oRegKey As RegistryKey\r\n\r\n\t'Set the subkey to work with\r\n\toRegKey = Registry.LocalMachine.OpenSubKey(\"SOFTWARE\", True)\r\n\r\n\t'Create a new subkey\r\n\toRegKey.CreateSubKey(\"MyApp\")\r\n\r\n\t'Set the subkey to work with\r\n\toRegKey = Registry.LocalMachine.OpenSubKey(\"SOFTWARE\\MyApp\", True)\r\n\r\n\t'Create new values\r\n\toRegKey.SetValue(\"Version\", \"1.0.0.0\")\r\n\toRegKey.SetValue(\"Author\", \"Dirk Schuermans\")\r\n\toRegKey.SetValue(\"Serial\", \"A1B2C3D4E5F6G7H8I9\")\r\n\r\n\t'Close the key\r\n\toRegKey.Close()\r\nEnd Sub<\/pre>\n<p><a href=\"http:\/\/dirk.schuermans.me\/?p=114\" target=\"_self\">&#8211; Continue to Part 3<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Welcome to the second part! In this part I&#8217;ll teach you how to write stuff away inside the Windows Registry.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[38],"tags":[],"_links":{"self":[{"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/posts\/104"}],"collection":[{"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=104"}],"version-history":[{"count":4,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/posts\/104\/revisions"}],"predecessor-version":[{"id":743,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/posts\/104\/revisions\/743"}],"wp:attachment":[{"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}