Introduction
The Windows Registry is a directory that acts as a central database of settings and options used in various versions of Microsoft Windows.TM It is used to store information about hardware, the operating system (OS), installed software, users and user profiles. The registry contains information about the operating system and software that can be referenced during program operation and has replaced the use of .ini files which were widely used before.
From a software development perspective in .NET the Registry shares its importance with configuration files, but is used as a central repository for information on many applications. In this article I am not going to explain much about the registry and its role in operation of the OS but hope to point out some of many things that you can do using the Registry.
Why the Registry?
According to Wikipedia, the Windows Registry was introduced to tidy up the profusion of per-program INI files that had previously been used to store configuration settings for Windows programs. These files tended to be scattered all over the system, which made them difficult to keep track of. In addition, from an application development point of view, it is often necessary to have a central repository to store application settings. Although in .NET we have the concept of configuration files, we cannot bypass use of the Registry because in the Registry we can also store information based on a user profile and we need the Registry to get OS and system hardware related information.
How to read from the Registry
The most straight forward way to read Registry values is using the Microsoft.Win32.Registry.GetValue()
method, although other methods in the Registry
class are also available. The method takes the following form:
Registry.GetValue(KeyName, ValueName, DefaultValue)
,
requiring three arguments and returning an object containing the value of the specified Registry key.
KeyName
: This is the full path of the Registry key, including HKEY_LOCAL_MACHINE
. ValueName
: In the Registry all information is stored in a key/value manner. For getting Registry value you need to pass its name. DefaultValue
: In case the value name is invalid, you this default value is returned, so that it is possible to determine that something is wrong and steps can be taken to handle the error.
How to write to the Registry
Writing to the Registry is also very straight forward using the Microsoft.Win32.Registry.SetValue()
method with the form:
Registry.SetValue(KeyName, ValueName, Value).
The arguments are straight forward.
KeyName
: This is the full path of the Registry key, including HKEY_LOCAL_MACHINE
. ValueName
: In the Registry all information is stored in a key/value manner. To set a Registry value you need to pass its name. Value
: The value that you want to store
Playing with the Registry
The Registry and the values stored in it are mostly used in the deployment of applications when it is necessary to check the system hardware and software configuration before installation or to change the system characteristics after installation. In the following section, I will discuss a few Registry values that might be useful in your future deployments.
Adding commands/items to your desktop context menu.
Suppose you have a desktop application that is used to open a new type of file (say .JDK files) and you want to add the ability to open .JDK files by right clicking on a .JDK file on your desktop and also add a "Open JDK File" menu item in your desktop context menu. This can be done as follows:
- Create a
.JDK
Registry key in HKEY_CLASS_ROOT
. - Change the default value to
JDKFILES
- Create a new key with name
JDKFILES
in HKEY_CLASS_ROOT
. - In the
JDKFILES
key add a subkey with name of SHELL
. All the windows shell commands will be taken from subkeys of SHELL
. - Add a subkey to
SHELL
with name Open
. This key's name will appear in the Explorer context menu as Open. I added this subkey because if file extension is not a common one, Windows will show the Open with dialog box. - Add a new subkey called
Command
to the Open
subkey. - Change the default value of this subbkey to "PATH TO YOUR APPLICATION EXECUTION FILE" "%1". For example, if you want to open your file with notepad, add following line to default:
"%SystemRoot%\system32\NOTEPAD.EXE" "%1"
- Add another subkey to
SHELL
named OPEN JDK FILES
. - Add a subkey to
OPEN JDK FILES
named Command
- Add any command that you want to execute after clicking on this item. If you want to pass this file name as parameter to an application, just type name of application followed by space and "%1"
To achieve the same effect when right clicking on a folder's context menu, all you need to do is:
- Add a subkey for your context menu item to
HKEY_CLASSES_ROOT\Directory\Background\shell
. For example JDK FOLDERS
- Add a subkey called
Command
to this subkey (JDK FOLDERS
) and put your command as the default value of this subkey.
Checking Screen Resolution
There are some cases in which you want to check screen resolution before installing an application. This can easily be done by accessing HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Hardware Profiles\0001\System\CurrentControlSet\SERVICES\IALM\DEVICE0
Hide Administrator logon screen at Startup
There are cases when you want to create a user with credentials in the system, for example to start some services, and put it in the administrators group to be able to do various tasks. In such cases, after creating the user, the user name will be displayed on the Windows Logon screen. This is highly undesirable, since the user is meant only to be used by an application and should not be exposed to end user.
You can hide a user by adding a DWORD
to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\SpecialAccounts\UserList\
, adding a value whose name is user name with a value of 0
. A value 0
causes the user to be left off of the Logon screen while a value of 1 will display the user on the Logon screen for Windows.
Add SQL Server alias for client network utility
Adding aliases for use with a client network utility is a very common task that is required in most deployments where the installed software needs to connect to a SQL Server database. This can be done by adding a value to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo
with the name that you want to assign as an alias and a value of "DBMSSOCN"+your alias
.
Conclusion
This short article is presented as an example of simple tasks that can be done using the Windows Registry. Note that the use of the Windows Registry is not limited to these simple tasks, but can also be used to access hardware, software settings, profiles and user specific information.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.