Click here to Skip to main content
15,887,898 members
Articles / Programming Languages / C#
Article

Registry handling with .NET

Rate me:
Please Sign up or sign in to vote.
4.83/5 (44 votes)
21 May 2002Ms-PL 195.8K   58   15
Shows with some code snippets how registry handling is a piece of cake with .NET

Introduction

Well, apparently the registry seems to have lost some of its importance with the arrival of .NET, at least that's the impression I seem to get. But luckily for us, Microsoft has given us two nice classes for doing just about anything we want to do with the registry. The classes are Microsoft.Win32.RegistryKey and Microsoft.Win32.Registry. They have both been put into the Microsoft.Win32 namespace as you can see because the registry is totally Microsoft Win32 specific. Without too much fuss, let's get into business and try and do some of the stuff we normally do with the registry.

Reading the registry

C#
//The Registry class provides us with the 
// registry root keys
RegistryKey rkey = Registry.LocalMachine;

//Now let's open one of the sub keys
RegistryKey rkey1=rkey.OpenSubKey(
    "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion");

//Now using GetValue(...) we read in various values 
//from the opened key
listBox1.Items.Add("RegisteredOwner :- " + 
    rkey1.GetValue("RegisteredOwner"));			
listBox1.Items.Add("RegisteredOrganization :- " +
    rkey1.GetValue("RegisteredOrganization"));
listBox1.Items.Add("ProductName :- " + 
    rkey1.GetValue("ProductName"));
listBox1.Items.Add("CSDVersion :- " + 
    rkey1.GetValue("CSDVersion"));
listBox1.Items.Add("SystemRoot :- " + 
    rkey1.GetValue("SystemRoot"));
    
rkey1.Close();

Writing to the registry

C#
rkey = Registry.CurrentUser;
//The second parameter tells it to open the key as writable
rkey1 = rkey.OpenSubKey("Software",true);

// Now we create our sub key [assuming you have enough 
// rights to edit this area of the registry]
RegistryKey rkey2 = rkey1.CreateSubKey("Tweety");

//Setting the various values is done using SetValue()
//I couldn't figure out how to set the value type yet :-(
rkey2.SetValue("Name","Tweety");
rkey2.SetValue("Age",24);
rkey2.Close();
rkey1.Close();

If you open regedit, you'll see that the new key has been added and the values have indeed been written correctly.

Image 1

Enumeration

Okay, we've read from and written into the registry. Now let's enumerate some values.

C#
rkey1 = rkey.OpenSubKey("Software\\Microsoft\\" +  
    "Internet Account Manager\\Accounts\\00000001");
    
string[] s_arr = rkey1.GetValueNames();

foreach(String s in s_arr)
{
	listBox1.Items.Add(s + " :- " + rkey1.GetValue(s));
}

rkey1.Close();

Image 2

Well, that's about it I guess. This was originally written as part of an internal tutorial. I didn't modify it too much except for taking better screenshots.

Thanks.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
United States United States
Nish Nishant is a technology enthusiast from Columbus, Ohio. He has over 20 years of software industry experience in various roles including Chief Technology Officer, Senior Solution Architect, Lead Software Architect, Principal Software Engineer, and Engineering/Architecture Team Leader. Nish is a 14-time recipient of the Microsoft Visual C++ MVP Award.

Nish authored C++/CLI in Action for Manning Publications in 2005, and co-authored Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on CodeProject.com and another 250+ blog articles on his WordPress blog. Nish is experienced in technology leadership, solution architecture, software architecture, cloud development (AWS and Azure), REST services, software engineering best practices, CI/CD, mentoring, and directing all stages of software development.

Nish's Technology Blog : voidnish.wordpress.com

Comments and Discussions

 
GeneralVery Good Pin
kulanthaivelu.v10-Nov-06 18:55
kulanthaivelu.v10-Nov-06 18:55 
Generalset/get default value Pin
Anonymous1-Sep-02 18:39
Anonymous1-Sep-02 18:39 
GeneralThank You Nish! Pin
Mazdak16-Apr-02 7:48
Mazdak16-Apr-02 7:48 
GeneralRe: Thank You Nish! Pin
Nish Nishant16-Apr-02 12:16
sitebuilderNish Nishant16-Apr-02 12:16 
GeneralGreat Article Pin
Nick Parker22-Mar-02 16:30
protectorNick Parker22-Mar-02 16:30 
GeneralRe: Great Article Pin
Nish Nishant22-Mar-02 16:36
sitebuilderNish Nishant22-Mar-02 16:36 
GeneralRegarding .NET and registry! Pin
Nish Nishant22-Mar-02 14:05
sitebuilderNish Nishant22-Mar-02 14:05 
GeneralRe: Regarding .NET and registry! Pin
Andy Smith22-Mar-02 19:24
Andy Smith22-Mar-02 19:24 
GeneralRe: Regarding .NET and registry! Pin
Nish Nishant22-Mar-02 20:50
sitebuilderNish Nishant22-Mar-02 20:50 
GeneralRe: Regarding .NET and registry! Pin
Jason Hooper23-Mar-02 5:29
Jason Hooper23-Mar-02 5:29 
GeneralRe: Regarding .NET and registry! Pin
James T. Johnson23-Mar-02 19:02
James T. Johnson23-Mar-02 19:02 
The .NET provided application configuration files are read once at execution then its no longer accessed. It is also read-only so you can't store something that could change for the application in them.

This leaves you to your own devices, however there are a few function calls to make it easier to properly place this type of stuff.

System.Environment.GetFolderPath() returns the path of the "special" folder you wish to retreive. To get the directory best suited for a system-wide application settings file pass in System.Environment.SpecialFolder.CommonApplicationData. To get the best directory for user settings pass in System.Environment.SpecialFolder.ApplicationData for roaming users or System.Environment.SpecialFolder.LocalApplicationData for non-roaming users.

Some have suggesting putting your settings into a DataSet, then using the Read/WriteXml methods to persist the data. But -- as always -- you are free to come up with your own solutions Smile | :)

James

Sonork ID: 100.11138 - Hasaki
"Smile your little smile, take some tea with me awhile.
And every day we'll turn another page.
Behind our glass we'll sit and look at our ever-open book,
One brown mouse sitting in a cage."
"One Brown Mouse" from Heavy Horses, Jethro Tull 1978

GeneralRe: Regarding .NET and registry! Pin
James T. Johnson25-Mar-02 14:48
James T. Johnson25-Mar-02 14:48 
GeneralRe: Regarding .NET and registry! Pin
Rama Krishna Vavilala25-Mar-02 15:27
Rama Krishna Vavilala25-Mar-02 15:27 
GeneralRe: Regarding .NET and registry! Pin
CareBear28-May-02 22:28
CareBear28-May-02 22:28 
GeneralRe: Regarding .NET and registry! Pin
Nish Nishant29-May-02 0:46
sitebuilderNish Nishant29-May-02 0:46 

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

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