5,691,626 members and growing! (13,499 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » .NET Framework » General     Beginner

Registry handling with .NET

By Nishant Sivakumar

Shows with some code snippets how registry handling is a piece of cake with .NET
C#, .NET, Win2K, WinXP, Windows, Visual Studio, Dev

Posted: 21 Mar 2002
Updated: 21 May 2002
Views: 109,056
Bookmarked: 35 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
46 votes for this Article.
Popularity: 6.17 Rating: 3.71 out of 5
0 votes, 0.0%
1
1 vote, 4.5%
2
2 votes, 9.1%
3
4 votes, 18.2%
4
15 votes, 68.2%
5

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

//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

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.

Enumeration

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

rkey1.Close();

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Nishant Sivakumar


Sitebuilder, Mvp
Nish is a real nice guy living in Atlanta, who has been coding since 1990, when he was 13 years old. Originally from sunny Trivandrum in India, he recently moved to Atlanta from Toronto and is a little sad that he won't be able to play in snow anymore.

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 - www.voidnish.com 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 - blog.voidnish.com

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.
Location: United States United States

Other popular .NET Framework articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 15 of 15 (Total in Forum: 15) (Refresh)FirstPrevNext
GeneralVery Goodmemberkulanthaivelu.v19:55 10 Nov '06  
Generalset/get default valuesussAnonymous19:39 1 Sep '02  
GeneralThank You Nish!memberMazdak8:48 16 Apr '02  
GeneralRe: Thank You Nish!memberNish [BusterBoy]13:16 16 Apr '02  
GeneralGreat ArticlememberNick Parker17:30 22 Mar '02  
GeneralRe: Great ArticlememberNish [BusterBoy]17:36 22 Mar '02  
GeneralRegarding .NET and registry!memberNish [BusterBoy]15:05 22 Mar '02  
GeneralRe: Regarding .NET and registry!memberAndy Smith20:24 22 Mar '02  
GeneralRe: Regarding .NET and registry!memberNish [BusterBoy]21:50 22 Mar '02  
GeneralRe: Regarding .NET and registry!memberJason Hooper6:29 23 Mar '02  
GeneralRe: Regarding .NET and registry!memberJames T. Johnson20:02 23 Mar '02  
GeneralRe: Regarding .NET and registry!memberJames T. Johnson15:48 25 Mar '02  
GeneralRe: Regarding .NET and registry!memberRama Krishna16:27 25 Mar '02  
GeneralRe: Regarding .NET and registry!memberCareBear23:28 28 May '02  
GeneralRe: Regarding .NET and registry!memberNish - Native CPian1:46 29 May '02  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 21 May 2002
Editor: James T. Johnson
Copyright 2002 by Nishant Sivakumar
Everything else Copyright © CodeProject, 1999-2008
Web16 | Advertise on the Code Project