{"id":157,"date":"2009-10-01T00:36:25","date_gmt":"2009-10-01T00:36:25","guid":{"rendered":"http:\/\/dirk.forbiddendream.be\/?p=157"},"modified":"2016-01-07T10:50:19","modified_gmt":"2016-01-07T10:50:19","slug":"working-with-windows-registry-part-5-everything-in-a-nutshell","status":"publish","type":"post","link":"https:\/\/dirk.schuermans.me\/?p=157","title":{"rendered":"Working with Windows Registry in VB.NET \u2013 Part 5: Everything in a nutshell"},"content":{"rendered":"<p>Welcome to the fifth and final part!<\/p>\n<p>In this post I&#8217;ll provide a complete overview of all the actions discussed in the previous 3 parts for those who don&#8217;t feel like reading through the entire explanation on each post \ud83d\ude1b<\/p>\n<p>Let&#8217;s get started shall we? \ud83d\ude42<\/p>\n<p><!--more--><\/p>\n<p>Table of contents<\/p>\n<ul>\n<li><a href=\"#writing\">Part 2 &#8211; Writing to the Registry<\/a><\/li>\n<li><a href=\"#reading\">Part 3 &#8211; Reading from the Registry<\/a><\/li>\n<li><a href=\"#deleting\">Part 4 &#8211; Deleting from the Registry<\/a><\/li>\n<li><a href=\"#everything\">Everything put together<\/a><\/li>\n<\/ul>\n<p><a name=\"writing\"><\/a><br \/>\n<span style=\"color: #ff0000;\">P<span style=\"text-decoration: underline;\"><strong>art 2 &#8211; Writing to the Registry<\/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 name=\"reading\"><\/a><br \/>\n<span style=\"color: #ff0000;\"><span style=\"text-decoration: underline;\"><strong>Part 3 &#8211; Reading from the Registry<\/strong><\/span><\/span><\/p>\n<pre name=\"code\" class=\"vb.net\">'Import correct namespace\r\nImports Microsoft.Win32\r\n\r\n'Procedure to read stuff from the Registry\r\nPublic Sub fnRead()\r\n\t'Declare variables\r\n\tDim oRegKey as RegistryKey\r\n\tDim sVersion, sAuthor, sSerial As String\r\n\r\n\t'Set the SubKey to work with\r\n\toRegKey = Registry.LocalMachine.OpenSubKey(\"SOFTWARE\\MyApp\")\r\n\r\n\t'Retrieve settings from Registry\r\n\tsVersion = oRegKey.GetValue(\"Version\")\r\n\tsAuthor = oRegKey.GetValue(\"Author\")\r\n\tsSerial = oRegKey.GetValue(\"Serial\")\r\nEnd Sub<\/pre>\n<p><a name=\"deleting\"><\/a><br \/>\n<span style=\"color: #ff0000;\"><span style=\"text-decoration: underline;\"><strong>Part 4 &#8211; Deleting from the Registry<\/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 name=\"everything\"><\/a><br \/>\n<span style=\"color: #ff0000;\"><span style=\"text-decoration: underline;\"><strong>Everything put together<\/strong><\/span><\/span><\/p>\n<p>This is a code snippet you can basicly copy \/ paste straight into your Visual Studio if you meet the following requirements:<\/p>\n<ol>\n<li>You have a form with 3 buttons called Form1\n<ol>\n<li>btnWrite<\/li>\n<li>btnRead<\/li>\n<li>btnDelete<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<pre name=\"code\" class=\"vb.net\">'Import correct namespace\r\nImports Microsoft.Win32\r\nPublic Class Form1\r\n    'Declare variables, scope: global\r\n    Dim oRegKey As RegistryKey\r\n\r\n    Private Sub btnWrite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWrite.Click\r\n        'Define what subkey to work with\r\n        oRegKey = Registry.LocalMachine.OpenSubKey(\"SOFTWARE\", True)\r\n\r\n        'Create a new subkey\r\n        oRegKey.CreateSubKey(\"MyApp\")\r\n\r\n        'Define what subkey to work with\r\n        oRegKey = Registry.LocalMachine.OpenSubKey(\"SOFTWARE\\MyApp\", True)\r\n\r\n        'Insert new values\r\n        oRegKey.SetValue(\"Test1\", \"test1\")\r\n        oRegKey.SetValue(\"Test2\", \"test2\")\r\n        oRegKey.SetValue(\"Test3\", \"test3\")\r\n\r\n        'Create a new subkey\r\n        oRegKey.CreateSubKey(\"TEST2\")\r\n\r\n        'Define what subkey to work with\r\n        oRegKey = Registry.LocalMachine.OpenSubKey(\"SOFTWARE\\MyApp\\TEST2\", True)\r\n\r\n        'Insert new values\r\n        oRegKey.SetValue(\"Test4\", \"test4\")\r\n        oRegKey.SetValue(\"Test5\", \"test5\")\r\n        oRegKey.SetValue(\"Test6\", \"test6\")\r\n\r\n        'Close the RegistryKey\r\n        oRegKey.Close()\r\n\r\n        MessageBox.Show(\"Registry entries have been created!\", \"Succes\", MessageBoxButtons.OK, MessageBoxIcon.Information)\r\n    End Sub\r\n\r\n    Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click\r\n        'Define variables\r\n        Dim sResult As String = \"\"\r\n\r\n        'Define what subkey to work with\r\n        oRegKey = Registry.LocalMachine.OpenSubKey(\"SOFTWARE\\MyApp\")\r\n\r\n        'Go over each value within the subkey\r\n        Try\r\n            For Each sValue As String In oRegKey.GetValueNames\r\n                'Add it to the results\r\n                sResult += sValue &amp; \" = \" &amp; oRegKey.GetValue(sValue) &amp; vbNewLine\r\n            Next\r\n        Catch ex As NullReferenceException\r\n            MessageBox.Show(\"It seems that the subkey SOFTWARE\\MyApp does not exist. Please create it first.\", \"ERROR\", MessageBoxButtons.OK, MessageBoxIcon.Error)\r\n\r\n            Exit Sub\r\n        End Try\r\n\r\n        'Show the results\r\n        MessageBox.Show(\"HKEY_LOCAL_MACHINE\\SOFTWARE\\MyAPP contains the following values:\" &amp; vbNewLine &amp; vbNewLine _\r\n                        &amp; sResult, \"Results\", MessageBoxButtons.OK, MessageBoxIcon.Information)\r\n\r\n        'Clear the results\r\n        sResult = \"\"\r\n\r\n        'Define what subkey to work with\r\n        oRegKey = Registry.LocalMachine.OpenSubKey(\"SOFTWARE\\MyApp\\TEST2\")\r\n\r\n        'Go over each value within the subkey\r\n        Try\r\n            For Each sValue As String In oRegKey.GetValueNames\r\n                'Add it to the results\r\n                sResult += sValue &amp; \" = \" &amp; oRegKey.GetValue(sValue) &amp; vbNewLine\r\n            Next\r\n        Catch ex As NullReferenceException\r\n            MessageBox.Show(\"It seems that the subkey SOFTWARE\\MyApp does not exist. Please create it first.\", \"ERROR\", MessageBoxButtons.OK, MessageBoxIcon.Error)\r\n\r\n            Exit Sub\r\n        End Try\r\n\r\n        'Show the results\r\n        MessageBox.Show(\"HKEY_LOCAL_MACHINE\\SOFTWARE\\MyAPP\\TEST2 contains the following values:\" &amp; vbNewLine &amp; vbNewLine _\r\n                        &amp; sResult, \"Results\", MessageBoxButtons.OK, MessageBoxIcon.Information)\r\n\r\n    End Sub\r\n\r\n    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click\r\n        'Define what subkey to work with\r\n        oRegKey = Registry.LocalMachine.OpenSubKey(\"SOFTWARE\", True)\r\n\r\n        'Delete the subkey\r\n        Try\r\n            oRegKey.DeleteSubKeyTree(\"MyApp\")\r\n\r\n            MessageBox.Show(\"Registry entries have been deleted!\", \"Succes\", MessageBoxButtons.OK, MessageBoxIcon.Information)\r\n        Catch ex As ArgumentException\r\n            MessageBox.Show(\"It seems that the subkey you're trying to delete does not exist. Please create it first\", \"Error\", MessageBoxButtons.OK, MessageBoxIcon.Error)\r\n        End Try\r\n\r\n        'Close the RegistryKey\r\n        oRegKey.Close()\r\n    End Sub\r\nEnd Class<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Welcome to the fifth and final part! In this post I&#8217;ll provide a complete overview of all the actions discussed in the previous 3 parts for those who don&#8217;t feel like reading through the entire explanation on each post \ud83d\ude1b Let&#8217;s get started shall we? \ud83d\ude42<\/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\/157"}],"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=157"}],"version-history":[{"count":4,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/posts\/157\/revisions"}],"predecessor-version":[{"id":783,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/posts\/157\/revisions\/783"}],"wp:attachment":[{"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=157"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=157"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=157"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}