C# – Windows registry manipulation

Standard

 

Most of the windows based programs are manipulating windows registry to save software specific settings. One of the examples system retain the resize settings and look and feel changed last access.

.Net has this feature to manipulate (open, read, write and delete) the windows registry using Microsoft.Win32 namespace. If you are beginner to Computer registry, please backup the registry then try with your testing key.

Using windows startup menu Start -> Run. Then type ‘regedit’ to open the Registry Editor. Backup the registry highlight either ‘My Computer’ (in registry windows first node) or specific key which one you wish to backup then choose the File menu from the registry windows then Export it. Give a meaningful name in save dialogue box including current date. (For example : BkFullRegistry24mar2010.reg)

In some cases, you can not access the registry, use IsolatedStorageFile using System.IO save and read the setting in Isolated location. I have not used this feature yet. It may useful ASP.NET application too either domain specific or user specific.

using System;

using Microsoft.Win32; 

namespace registry

{

class Program

{

static void Main(string[] args)

{

//If you are not much familier with windows registry, do not try this

//RegistryKey rk = createRegistryKey(@"Software\Microsoft", "TestKey");

//setRegistryValue(rk , "thiruTestKeyName", "thiruTestValue");

deleteRegistryKey(@"Software\Microsoft", "TestKey");

}

static RegistryKey createRegistryKey(string rootKey, string strSubKey)

{

RegistryKey rk = Registry.LocalMachine;

//RegistryKey rkSoftMicro = rk.OpenSubKey("Software").OpenSubKey("Microsoft");

RegistryKey rkSoftSlashMicro = rk.OpenSubKey(rootKey,true).CreateSubKey(strSubKey);

return rkSoftSlashMicro;

}

static bool setRegistryValue(RegistryKey rk, string registryName, string strValue )

{

rk.SetValue(registryName, strValue);

return true;

} 

static bool deleteRegistryKey(string rootKey, string strSubKey)

{

// Create new testing registry key and play with that one  

RegistryKey rk = Registry.LocalMachine;

//RegistryKey rkSoftMicro = rk.OpenSubKey("Software").OpenSubKey("Microsoft");

rk.OpenSubKey(rootKey, true ).DeleteSubKey(strSubKey);

return true;

}

}

}

 

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