Click here to Skip to main content
Click here to Skip to main content

Windows Registry

, 4 Aug 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
A few tips concerning the Windows Registry

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:

  1. Create a .JDK Registry key in HKEY_CLASS_ROOT.
  2. Change the default value to JDKFILES
  3. Create a new key with name JDKFILES in HKEY_CLASS_ROOT.
  4. In the JDKFILES key add a subkey with name of SHELL. All the windows shell commands will be taken from subkeys of SHELL.
  5. 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.
  6. Add a new subkey called Command to the Open subkey.
  7. 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"
  8. Add another subkey to SHELL named OPEN JDK FILES.
  9. Add a subkey to OPEN JDK FILES named Command
  10. 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:

  1. Add a subkey for your context menu item to HKEY_CLASSES_ROOT\Directory\Background\shell. For example JDK FOLDERS
  2. 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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

interface Mirror
Team Leader
United Arab Emirates United Arab Emirates
No Biography provided

Comments and Discussions

 
GeneralMy vote of 2 Pinmembercccfff7776-Aug-10 4:40 
GeneralMy vote of 1 Pinmemberkarabax5-Aug-10 6:39 
GeneralVote of 1 PinmemberKevin Marois5-Aug-10 5:39 
GeneralMy vote of 2 PinmemberToli Cuturicu4-Aug-10 23:59 
GeneralMy vote of 1 PinmemberAxelM4-Aug-10 20:34 
GeneralError Create Sub Key on windows Vista Pinmembernmhai834-Dec-08 18:15 
GeneralRe: Error Create Sub Key on windows Vista PinmemberZiv G4-Aug-10 22:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.141022.2 | Last Updated 4 Aug 2010
Article Copyright 2007 by interface Mirror
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid