{"id":144,"date":"2009-10-01T00:09:00","date_gmt":"2009-10-01T00:09:00","guid":{"rendered":"http:\/\/dirk.forbiddendream.be\/?p=144"},"modified":"2016-01-07T10:50:19","modified_gmt":"2016-01-07T10:50:19","slug":"working-with-windows-registry-part-4-deleting-from-the-registry","status":"publish","type":"post","link":"https:\/\/dirk.schuermans.me\/?p=144","title":{"rendered":"Working with Windows Registry in VB.NET \u2013 Part 4: Deleting from the Registry"},"content":{"rendered":"<p>Welcome to the fourth part!<\/p>\n<p><strong><span style=\"color: #ff0000;\">Before I continue with this part, I can&#8217;t stress enough that you have to stay extremely focused when deleting stuff from the Registry. One small mistake such as a typo and you could end up having to reinstall your entire PC.<\/span><\/strong><\/p>\n<p>So like I said, this part will handle the deletion part. It&#8217;s obvious that when you don&#8217;t use a program anymore you don&#8217;t want to have Registry entries for it.<\/p>\n<p><!--more--><br \/>\n<a href=\"#summary\">Proceed to summary of this post<\/a><\/p>\n<p>As in the previous parts, we&#8217;ll need to import the namespace and declare the oRegKey variable:<\/p>\n<pre name=\"code\" class=\"vb.net\">'Import correct namespace\r\nImports Microsoft.Win32\r\n\r\n'Declare variable within procedure\r\nDim oRegKey As RegistryKey<\/pre>\n<p>Now that we have those, we&#8217;ll have to define what subkey to work with:<\/p>\n<pre name=\"code\" class=\"vb.net\">'Define what subkey to work with:\r\noRegKey = Registry.LocalMachine.OpenSubKey(\"SOFTWARE\", True)<\/pre>\n<p>From this point on it&#8217;s fairly simple. You have 2 ways to go about this:<\/p>\n<ul>\n<li>Delete the subkey and it&#8217;s values<\/li>\n<li>Go over each value in the subkey and delete them one by one<\/li>\n<\/ul>\n<p>I&#8217;ll handle the easiest and most commonly used way: &#8220;Delete the subkey and it&#8217;s values&#8221;:<\/p>\n<p><span style=\"color: #ff0000;\">*NOTE: If you have another subkey within your primary subkey, then you&#8217;ll have to use a slightly different command. I&#8217;ll explain this in a bit.<\/span><\/p>\n<pre name=\"code\" class=\"vb.net\">'Delete the entire subkey and it's values\r\noRegKey.DeleteSubKey(\"MyApp\")<\/pre>\n<p>If you run that the entire subkey and it&#8217;s values will be deleted.<\/p>\n<p>Now let&#8217;s say we have our primary subkey called &#8220;MyApp&#8221; and within that subkey we&#8217;ve created another subkey called &#8220;Data&#8221;.<\/p>\n<p>The subkey will look like this: &#8220;HKEY_LOCAL_MACHINE\\SOFTWARE\\MyApp\\Data\\&#8230;&#8221;<\/p>\n<p>If we use the method I just explained, you won&#8217;t be able to delete the subkey at all. So we&#8217;ll have to invoke a slightly different command:<\/p>\n<pre name=\"code\" class=\"vb.net\">'Delete the entire subkey and any possible subkeys of our primary key\r\noRegKey.DeleteSubKeyTree(\"MyApp\")<\/pre>\n<p><span style=\"color: #ff0000;\">*NOTE: This approach also works if you have a primary subkey with no other subkeys within that subkey!<\/span><\/p>\n<p>That&#8217;s all there is to it. You&#8217;re now able to delete stuff from the Windows Registry \ud83d\ude42<\/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\nPublic Sub fnDelete()\r\n\t'Declare variables\r\n\tDim oRegKey As RegistryKey\r\n\r\n\t'Define subkey to work with\r\n\toRegKey = Registry.LocalMachine.OpenSubKey(\"SOFTWARE\", True)\r\n\r\n\t'Delete our subkey\r\n\toRegKey.DeleteSubKeyTree(\"MyApp\")\r\n\r\n\t'Close the RegistryKey\r\n\toRegKey.Close()\r\nEnd Sub<\/pre>\n<p><a href=\"http:\/\/dirk.schuermans.me\/?p=157\" target=\"_self\">&#8211; Continue to Part 5<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Welcome to the fourth part! Before I continue with this part, I can&#8217;t stress enough that you have to stay extremely focused when deleting stuff from the Registry. One small mistake such as a typo and you could end up having to reinstall your entire PC. So like I said, this part will handle the &hellip; <a href=\"https:\/\/dirk.schuermans.me\/?p=144\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Working with Windows Registry in VB.NET \u2013 Part 4: Deleting from the Registry<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[38,6],"tags":[],"_links":{"self":[{"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/posts\/144"}],"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=144"}],"version-history":[{"count":4,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/posts\/144\/revisions"}],"predecessor-version":[{"id":784,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/posts\/144\/revisions\/784"}],"wp:attachment":[{"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=144"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=144"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=144"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}