Working with the Registry

My.Computer provides fast access to Windows Registry. It exposes a Registry property wrapping lots of functionalities of Microsoft.Win32.Registry class for faster work. My.Computer.Registry offers some properties, of type Microsoft.Win32.RegistryKey, representing the most important areas of the Registry, such as HKEY_LOCAL_MACHINE (wrapped by the LocalMachine property), HKEY_ALL_USER (wrapped by Users), HKEY_CURRENT_USER (wrapped by CurrentUser), and HKEY_CLASSES_ROOT (wrapped by ClassesRoot). All of them provide methods for creating subkeys, querying, deleting, and setting values within subkeys. For example, the following code (which requires an Imports Microsoft.Win32 directive) creates a subkey in the HKEY_CURRENT_USERSoftware key, providing a company name and the application name. The code also sets permissions for writing/reading the key and its eventual subkeys:

image

Because the CreateSubKey returns a RegistryKey object, you can invoke instance members from this type. For example, you can add values to the new key by invoking the SetValue method as follows:

'Value-name, actual value
regKey.SetValue("MyValue", 1)

You get the value of the desired subkey by invoking the GetValue method as follows:

image

Remember that GetValue returns Object, so you need to perform an explicit conversion according to the value type you expect. You also have the ability to determine what kind of value is associated to a value name. This can be accomplished by getting a RegistryValueKind value, such as DWord, Binary, String, and QWord, (which is an enumeration from Microsoft.Win32) via the GetValueKind method so that you can also be more precise when requiring values:

image

There is also a GetNames method that returns an array of strings, each representing a value in the specified subkey if more than one value is stored within the subkey. The following code instead removes the previously created value:

regKey.DeleteValue("MyValue")

Remember to close the Registry key when you do not use it anymore:

regKey.Close()

Finally, you can delete a subkey by invoking the DeleteSubKey as follows:

image

Other than the mentioned properties about Registry areas, My.Computer.Registry exposes just two interesting methods, SetValue and GetValue, which basically require you to specify long strings and that usually can be replaced by the same-named methods of the instance of RegistryKey. By the way, with My.Computer.Registry you can perform lots of tasks onto the system Registry writing code easier and faster.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.167.114