Get Connection String from Windows Registry using C#.net

First of first how to open windows registry editor ?

Open Run Menu (Windows + R) and type regedit the below screen is open.

clip_image002

(Figure 1)

here I am created the simple application using windows C#.net 2.0.

image

steps:

1) Create Simple Windows Application using C#. add the namespace

using Microsoft.Win32;

2) put the below control on the form.

Control Control Id Control Value
label label1 Enter Connection String
label lblGetConnectionString
TextBox txtConnectionString
Button btnSaveConnectionStringInDB Save Connection String In Data
Button btnRetriveConnectionStringFromRegistry Retrive Connection String From Windows Registry

3) in the Figure 1 Local Machine Contain Five Key

1) Hardware 2) Sam 3) Security 4) Software 5) System.

in the figure 1 only one key is display. now we code and then new key is added under the

HKEY_LOCAL_MACHINE. in below screen the brief idea is given.

image

4) Now Put the Below code in to .CS File.

4.1) Declare the global variable

//configuration data for the local machine
   //Windows registry base key HKEY_LOCAL_MACHINE
   RegistryKey objRegistryKey = Registry.LocalMachine;
4.2) on the button click (btnSaveConnectionStringInDB) put below code.
private void btnSaveConnectionStringInDB_Click(object sender, EventArgs e)
   {
     try
     {
       //now set the new key under the base key
       //argument 1) Name Of The Key (MyConnectionString)
       //         2) Key (TextBox Enter Value)
       //         3) value type (string)
       //now i am use hardcoded key value
       objRegistryKey.SetValue("MyConnectionString", txtConnectionString.Text, RegistryValueKind.String);
       MessageBox.Show("Key Is Sucessfully Registered");
     }
     catch (Exception ex)
     {
       MessageBox.Show("Can Not Store Data In Registry" + ex.Message.ToString());
     }
   }

4.3) on the button click (btnRetriveConnectionStringFromRegistry) put below code.

private void btnRetriveConnectionStringFromRegistry_Click(object sender, EventArgs e)
{
  string values = (string)objRegistryKey.GetValue("MyConnectionString");
  //display the connection string value in the label
  lblGetConnectionString.Text = values;
}

5) Add Key In to Windows Registry.

image

After the get message open the windows registry.

image

entered key is added successfully please check the figure1 and last this figure.

6) Get the Value from the RegistryKey (MyConnectionString)

image

Thnx

Advertisements

6 comments

  1. Interesting thing happens when I do this. It saves the registry entry just fine, but when I restart my machine, it clears those values from the registry. Any thoughts?

    1. hi Albert,
      can you give me hint about your os? because i have check this code in WinXP and Win2003 Server. this is not happening with me.

      let’s try again.

      thnx

  2. Good one worked fine.

  3. please can u tell me please… if i have windows based application and its work properly in LAN – i have 1 server which sotre a Database 1 shreable location i can use from evry client host..
    now…i want to connect to out side my Lan (network) at other location located database which changes i have to done if i want to connect remote location database…which communicate via inter net ….

    thnx…..
    reply me on genuine.forever@hotmail.com
    please mention sub : Asp.net

  4. Dungnv · · Reply

    what store connection string in registry has meaning?

  5. tueyntc · · Reply

    what store connection string in registry has meaning.?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s