.comment-link {margin-left:.6em;}

.code

.. Hello Earthling .. \o/ :)

Let me blow your mind
Friday, December 17, 2004

Google Suggest Beta

.


Reading/Writing Registry in .Net
Tuesday, December 07, 2004

One of my friends asked me how he can make his application start whenever he logs into his computer. Actually he asked this question in some forum and emailed me the link to it, but when I was about to answer that question that site wanted me to register at the site, which I hate to do. I have like so many user names many of which I don’t even ever use. So I emailed my friend back a reply that I can’t answer his question there, instead come to my blog and read the answer.


So here it is: the registry in .net is available in the Microsoft.Win32 namespace. Why is it not inside the System namespace like many other classes in the .net framework? Thats because there is no concept of registry in the .net framework. For .net the GAC (global assembly cache) gives all those facilities. So the .net framework provides a different namespace to clearly differentiate registry classes. Anyway, the main class that you play with while working with registry is called the RegistryKey. It’s got nice methods, very easy to work with and very easy to understand. The class that gives access to different keys is Registry. It’s got some public fields, all of which gives you a live object of type RegistryKey. The difference in these fields is that the key is different. Like HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, etc.


Lets say we want to see what are the entries in the recent list of our media player. I need to know which key this information I can find in. It’s called the HKEY_CURRENT_USER key, and programmatically I can access it using the following code:


RegistryKey key= Registry.CurrentUser;

(don’t forget to add the using Microsoft.Win32;)

The registry path to the recent file list of media player is “software\microsoft\mediaplayer\player\recentfilelist”. So what we’ll do is simply call a OpenSubKey method on the object of RegistryKey, and get the list of all the enteries of media player recent list. This can be done through the following code:


string mediaplayerarea = @"software\microsoft\mediaplayer\player\recentfilelist";


RegistryKey key= Registry.CurrentUser ;

RegistryKey subkey= key.OpenSubKey(mediaplayerarea, true);

foreach ( string tmp in subkey.GetValueNames() )

{

    Console.WriteLine("value name is :{0}, value is :{1}",tmp, subkey.GetValue( tmp ) );

}


So if you read the code you see that I’m calling the GetValue method to get the data of the value. Can anything be simpler than this?

Writing to the registry is as easy as reading. All you have to do is call the SetValue method instead the GetValue and your value will be written, provided the key opened is writeable which you specify in the second parameter to your call to OpenSubKey. Now lets say you want to make an entry to the registry which will make your application run everytime you log in to the computer. This information is also inside the HKEY_CURRENT_USER key. So you can utilize the code above to write to the startup registry entry.


void addtostartup(string filename)

{

    string startkeyspath=@"software\microsoft\windows\currentversion\run";

    RegistryKey key= Registry.CurrentUser ;

    RegistryKey subkey= key.OpenSubKey(startkeyspath, true);

    subkey.SetValue("myapp", filename );

}


After this code executes the filename will be present in your Run key and would be launched as soon as you log in to the computer.


To see the entries and experiment with them you can use the RegEdit.exe (run from the command prompt). But you make sure that you do not write to or read the registry of applications that you do not understand what they are there for. If registry enteries are corrupted, your applications may start behaving abnormally.







Blogs:

MSDN Blogs
Joel Pobar
Don Syme

Friends:
Adeel
Aqeel
#Fahad
Haroon
Omer
Muhammad Ali
Lahore Food Blog

Links:
Rotor
CodeGuru
Mozilla
OpenSourceNokia
Tech Blog

Languages:
IronPython
F#

Archives

June 2004   July 2004   August 2004   September 2004   October 2004   November 2004   December 2004   February 2005   April 2005   June 2005   July 2005   September 2005   October 2005   November 2005   June 2007   December 2007   January 2008   March 2008   April 2008   June 2008   October 2008   February 2009   May 2009   June 2009   July 2009   August 2009   March 2011   June 2011   July 2011   September 2011   October 2011   November 2011   December 2012   May 2013   October 2013   May 2014   March 2015   July 2015   August 2015   December 2015   March 2016   July 2016   October 2016   November 2016  

This page is powered by Blogger. Isn't yours?