How to read Windows Registry?


In this article, I will explain how to read Windows Registry. First part of this article explains how to add your own registry entries to test the program.

Here are the step:

Add Registry Data

Following text is a actual registry information. Copy and paste this content in a text file and save as with extention .reg. and double click on the file.

/*Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\TAW\BSE]
"DSN"="TAWReports"
"User"="TAW1"
"Password"="taw1.1"
"Server"="dbserver"
"IP"=""
*/


Read the Registry Data

  1. Create two RegistryKey variables.
  2. Create object od sealed class as shown below with first parameter as HKEY (main root key name) and second as "" for local machine.
  3. Create sub key from where one would like to read information.
  4. Use Getvalue method of RegistryKey class for reading data for a particular node key information. Here DSN,Server,Password are nodes.

This source code reads the registry data which you have just added to your registry.

namespace CONAPP
{
using System;
using Microsoft.Win32;
public class Class1
{
public Class1()
{
//
// TODO: Add Constructor Logic here
//
}
public static int Main(string[] args)
{
RegistryKey SUBKEY;
RegistryKey TAWKAY = RegistryKey.OpenRemoteBaseKey(Microsoft.Win32.RegistryHive.CurrentUser,"");
string subkey = "Software\\TAW\\BSE";
SUBKEY = TAWKAY.OpenSubKey(subkey);
object dsn = SUBKEY.GetValue("DSN");
object user = SUBKEY.GetValue("user");
object password = SUBKEY.GetValue("password");
object server = SUBKEY.GetValue("server");
return 0;
}
}
}

Up Next
    Ebook Download
    View all
    Learn
    View all