Click here to Skip to main content
Click here to Skip to main content

Registry handling with .NET

, 21 May 2002 Ms-PL
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 is a real nice guy who has been writing code since 1990 when he first got his hands on an 8088 with 640 KB RAM. Originally from sunny Trivandrum in India, he has been living in various places over the past few years and often thinks it’s time he settled down somewhere.
Nish has been a Microsoft Visual C++ MVP since October, 2002 - awfully nice of Microsoft, he thinks. He maintains an MVP tips and tricks web site - where you can find a consolidated list of his articles, writings and ideas on VC++, MFC, .NET and C++/CLI. Oh, and you might want to check out his blog on C++/CLI, MFC, .NET and a lot of other stuff -
Nish loves reading Science Fiction, P G Wodehouse and Agatha Christie, and also fancies himself to be a decent writer of sorts. He has authored a romantic comedy Summer Love and Some more Cricket as well as a programming book – Extending MFC applications with the .NET Framework.
Nish's latest book C++/CLI in Action published by Manning Publications is now available for purchase. You can read more about the book on his blog.
Despite his wife's attempts to get him into cooking, his best effort so far has been a badly done omelette. Some day, he hopes to be a good cook, and to cook a tasty dinner for his wife.

Comments and Discussions

GeneralVery Good Pinmemberkulanthaivelu.v10-Nov-06 19:55 
Generalset/get default value PinsussAnonymous1-Sep-02 19:39 
GeneralThank You Nish! PinmemberMazdak16-Apr-02 8:48 
GeneralRe: Thank You Nish! PinmemberNish [BusterBoy]16-Apr-02 13:16 
GeneralGreat Article PinmemberNick Parker22-Mar-02 17:30 
GeneralRe: Great Article PinmemberNish [BusterBoy]22-Mar-02 17:36 
GeneralRegarding .NET and registry! PinmemberNish [BusterBoy]22-Mar-02 15:05 
GeneralRe: Regarding .NET and registry! PinmemberAndy Smith22-Mar-02 20:24 
GeneralRe: Regarding .NET and registry! PinmemberNish [BusterBoy]22-Mar-02 21:50 
GeneralRe: Regarding .NET and registry! PinmemberJason Hooper23-Mar-02 6:29 
It would absolutely make my day if the use of .conf files became standard on the Windows platform Smile | :)
- Jason
(SonorkID 100.611)
Veni, vidi, VC
      - I came, I saw, Visual C
GeneralRe: Regarding .NET and registry! PinmemberJames T. Johnson23-Mar-02 20:02 
GeneralRe: Regarding .NET and registry! PinmemberJames T. Johnson25-Mar-02 15:48 
GeneralRe: Regarding .NET and registry! PinmemberRama Krishna25-Mar-02 16:27 
GeneralRe: Regarding .NET and registry! PinmemberCareBear28-May-02 23:28 
GeneralRe: Regarding .NET and registry! PinmemberNish - Native CPian29-May-02 1: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.150327.1 | Last Updated 22 May 2002
Article Copyright 2002 by Nish Nishant
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid