A reader asked how to write a program that would be able to access an installed program no matter where it was on a PC. Most programs are in the standard location, C:\Program Files. But occasionally, people will choose a custom location. It might only be a different partion of the hard drive, but that's enough to stop a program that depends on a fixed location.
Fortunately, VB.NET has you covered again! All of the locations of installed programs are in the registry and it's not that hard to access them. The general method uses the .NET objects Registry and RegistryKey, both part of the Microsoft.Win32 namespace. You'll normally have to add a statement ...
Imports Microsoft.Win32
... to the top of your code to use them.
The Registry object is, as you might expect, 'the registry' in Windows. .NET exposes these keys using the Registry object:
- CurrentUser - Stores information about user preferences (HKEY_CURRENT_USER)
- LocalMachine - Stores configuration information for the local machine (HKEY_LOCAL_MACHINE).
- ClassesRoot - Stores information about types (and classes) and their properties (HKEY_CLASSES_ROOT).
- Users - Stores information about the default user configuration (HKEY_USERS).
- PerformanceData - Stores performance information for software components (HKEY_PERFORMANCE_DATA).
- CurrentConfig - Stores non-user-specific hardware information (HKEY_CURRENT_CONFIG).
- DynData - Stores dynamic data (HKEY_DYN_DATA).
The idea is to access a subkey of one of these keys and then do what you need to do using either the GetValue or SetValue method of this object or one of the many more properties and methods of one of the objects above or the RegistryKey object. In general, it's easier and recommended to return a RegistryKey object from Registry.<keyname>.<method> and then use the methods and properties of that RegistryKey object.
If that was as clear as mud, then maybe this example will help.
To return the path to the Excel 2007 program on my computer, I can code this:
Dim excelPath As String = _
Registry.GetValue( _
"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\excel.exe", _
"Path", "Key does not exist")
If Excel isn't installed, then excelPath will contain "Key does not exist". In my case, it returns "C:\Program Files\Microsoft Office\Office12\". (That long string will normally bleed onto a second line because the space available on the web page isn't wide enough. But it must be all on one line.)
But I can also code this:
Dim rk As RegistryKey = Registry.LocalMachine.OpenSubKey( _
"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\excel.exe")
Dim excelPath As String = rk.GetValue("Path")
The advantage of the second example is that RegistryKey has a lot more flexibility in case you want to do something else.
If you explore the properties and methods of these objects, you'll discover that you can do just about anything you need to do using one of them. For example, I mentioned at the beginning of this article that people will sometimes choose a custom location for their program files. You can find that in the registry too. Just check out:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\ProgramFilesDir
A variation of the code above should work just fine for you.
