Click here to Skip to main content
11,805,316 members (64,451 online)
Click here to Skip to main content

Registry handling with .NET

, 21 May 2002 Ms-PL 171.4K 56
Rate this:
Please Sign up or sign in to vote.
Shows with some code snippets how registry handling is a piece of cake with .NET


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

//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 :- " + 
listBox1.Items.Add("RegisteredOrganization :- " +
listBox1.Items.Add("ProductName :- " + 
listBox1.Items.Add("CSDVersion :- " + 
listBox1.Items.Add("SystemRoot :- " + 

Writing to the registry

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 :-(

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


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

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));


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.



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


About the Author

Nish Nishant
United States United States
Nish Nishant is a Software Architect/Consultant based out of Columbus, Ohio. He has over 15 years of software industry experience in various roles including Lead Software Architect, Principal Software Engineer, and Product Manager. Nish is a recipient of the annual Microsoft Visual C++ MVP Award since 2002 (13 consecutive awards as of 2014).

Nish is an industry acknowledged expert in the Microsoft technology stack. He authored
C++/CLI in Action for Manning Publications in 2005, and had previously co-authored
Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on and another 250+ blog articles on his
WordPress blog. Nish is vastly experienced in team management, mentoring teams, and directing all stages of software development.

Contact Nish : You can reach Nish on his google email id voidnish.

Website and Blog

You may also be interested in...

Comments and Discussions

GeneralRe: Regarding .NET and registry! Pin
James T. Johnson23-Mar-02 19:02
memberJames T. Johnson23-Mar-02 19:02 
GeneralRe: Regarding .NET and registry! Pin
James T. Johnson25-Mar-02 14:48
memberJames T. Johnson25-Mar-02 14:48 
GeneralRe: Regarding .NET and registry! Pin
Rama Krishna25-Mar-02 15:27
memberRama Krishna25-Mar-02 15:27 
GeneralRe: Regarding .NET and registry! Pin
CareBear28-May-02 22:28
memberCareBear28-May-02 22:28 
GeneralRe: Regarding .NET and registry! Pin
Nish - Native CPian29-May-02 0:46
memberNish - Native CPian29-May-02 0:46 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151002.1 | Last Updated 22 May 2002
Article Copyright 2002 by Nish Nishant
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid