Click here to Skip to main content
Email Password   helpLost your password?


Registry:

Registry is a directory that acts as a central database of settings and options used in MS-Windows different versions. It is used to store information about hardware, Operating system, installed software's, users and their profile. Registry contains information about operating system and software's referencing during their operation. It has replaced use of .ini files which was being used widely before but from software development perspective in .NET it shares its importance with configuration files. But it still is being used as a container of central information of many applications. In this article I am not going to explain much about Registry and its role in operation of OS but my aim is to remind you few of many things that you can do by playing with registry.

Why Registry:

According to Wikipedia 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 track. Apart of this general usage, if we talk from application development point of view, there is always a high demand to have a central repository to store application settings. Although in .NET we have concept of configuration files but still we can not bypass use of Registry because in Registry we can store information based on user profile and we need Registry to get OS and System Hardware related information.


How to read from Registry:


Reading values from Registry is very simple. With help of following method

Microsoft .Win32 .Registry .GetValue ( @"HKEY_LOCAL_MACHINE\..." , "ValueName" , "DefaultValue" )

Registry.GetValue() method takes three arguments to return you back an object containing value of Registry key.

First argument is Keyname. It is full path of your Registry key. It contains HKEY_LOCAL_MACHINE also. This is the most straight forward way to get Registry value otherwise you can use other methods in RegistryKey class.

Second argument is Value name. In Registry every all the information are stored in a key/value manner. For getting Registry value you need to pass its name.

Third argument is a default value mentioned by you. In case your value name is invalid you get your default value as return. In this way you can find that something is wrong and take further steps to handle error.

How to write to Registry:

Writing to registry is also very straight forward as you can use Registry.SetValue(). Registry.SetValue method again takes three arguments.

Microsoft .Win32 .Registry.SetValue ( KeyName, ValueName, Value).

Arguments are quite clear. Keyname is name of key in which values exists in that. ValueName is name of value that you want to store value for it and value is the value that you want to store.


When to Play with Registry:

Registry and values stored in it come into scene mostly at deployment phase of your application. There are circumstances that you want to check system hardware, software configuration before installing application. Or you want to change system characteristics after installation. In following section, I will introduce/recall few Registry values that maybe become in your future deployments.

Play with Registry:

Adding commands/items to your desktop context menu.

Suppose you have a desktop application used to open new type of file (let call it .JDK files) created by you and you want to add functionality of opening .JDK files in your application by right clicking on .JDK files on your desktop. And you want to see "Open JDK File" item in your desktop context menu. This is what you need to do:

1) Create a .JDK Registry key in your HKEY_CLASS_ROOT.

2) Change value of Default value to JDKFILES


3) Create a new key with name of JDKFILES in HKEY_CLASS_ROOT.

4) In JDKFILES key add a subkey with name of SHELL. All the windows shell commands will be taken from shubkey's of SHELL.

5) Add a subkey to SHELL with name of Open. This key's name will be appeared in Explorer context menu as Open. I added this subkey because if file extension is not a common one, Windows would show OPEN WITH dialog box.

6) Add a new subkey called Command to Open subkey.

7) Change value of Defauly key of this subbkey to "PATH TO YOUR APPLICATION EXECUTION FILE" separated by space to "%1". Suppose of you want to open your file with notepad add following line to default
"%SystemRoot%\system32\NOTEPAD.EXE" "%1"

8) Add another subkey to SHELL with name of "OPEN JDK FILES".

9) Add a subkey to OPEN JDK FILES with name of 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 any application, just type name of application followed by space and "%1"

If you want achieve same effect for right clicking on folder's context menu, all you need to do is:

1) Add a subkey presenting your context menu item to
HKEY_CLASSES_ROOT\Directory\Background\shell. For example "JDK FOLDERS"

2) Then add a subkey called command to "JDK FOLDERS" and put your command in default value of this subkey.



Checking Monitor Screen Resolution:

There are some cases in which you want to check screen resolution before installing application.

You can check resolution by accessing

HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Hardware Profiles\0001\System\CurrentControlSet\SERVICES\IALM\DEVICE0

Hiding Administrator logon screen at the startup:

There are cases when you want to create a user in system for example to start some services with its credentials or any other reason and at the same time you want to put it in administrators group to be able to do various task. In such cases after creating user you will see that his name will be displayed in Logon screen of windows which is highly undesirables because that user is created to be used by application and it is not supposed to be exposed to end user.

You can hide that user by adding a DWORD to HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Windows NT \ CurrentVersion \ Winlogon \ SpecialAccounts \ UserList \ and adding a value whose name is user name and value is 0. Value 0 causes that user be invisible in Logon screen. If you change value from 0 to 1 you will see new user will appear to logon screen of your windows operating system.

Adding SQL Server alias for client network utility:

Adding aliases to using client network utility is a very common task that is required to be done in most of deployments in which installed software needs to connect to SQL Server database.

By adding a value to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo with name is what you want to assign as alias name and value be "DBMSSOCN"+your alias.


Conclusion:

This small article was as a pack of simple tasks that can be done by help of windows Registry. Windows Registry is not limited to these; you can do a range of different things to access, harware, software settings, profiles and user specific information.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralError Create Sub Key on windows Vista
nmhai83
19:15 4 Dec '08  
I have a question. Look code below:

RegistryKey RegKey = Registry.ClassesRoot.CreateSubKey(".cnfg");
RegKey.SetValue("", "cnfg files");
RegKey.Close();


this code can run on windows XP but can't windows vista.
who have the same above code that can run windows vista. Can give it for me?
Help me, please. Thanks!


Last Updated 31 Aug 2007 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010