Microsoft KB Archive/816163

= How to write your own registry accessing functions by using base class libraries in Visual C# .NET or in Visual C# 2005 =

Article ID: 816163

Article Last Modified on 11/26/2007

-

APPLIES TO


 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Visual C# 2005 Express Edition

-





For a Microsoft Microsoft Visual Basic .NET version of this article, see 316151.

This article refers to the following Microsoft .NET Framework Class Library namespaces:
 * Microsoft.Win32

IN THIS TASK

 * SUMMARY
 * Using the Registry and RegistryKey Classes
 * Complete Code Listing
 * REFERENCES



SUMMARY
This article demonstrates how to use Visual C# to access the registry. To do this, write your own registry accessing functions using the .NET Framework base class libraries.

back to the top



Using the Registry and RegistryKey Classes
To access the registry, use the Registry class and the RegistryKey class in the Microsoft.Win32 namespace.
 * The Registry class supplies the base registry keys to access values and subkeys in the registry. It has static fields, such as CurrentUser, to represent the root registry key.
 * The RegistryKey class represents a key level node in the Windows registry. The class is a registry encapsulation that enables read and write operations to the registry through the GetValue method and the SetValue method of the RegistryKey class.

To create a console project to read and write to the registry by using the Registry class and the RegistryKey class, follow these steps:  Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. On the File menu, point to New3, and then click Project. Under Project Types, select Visual C# Projects. Under Templates, select Console Application, and then click OK.

Note In Visual Studio 2005, click Visual C# under Project Types, click Console Application under Templates, and then click OK.  Add the following namespace to Class1.cs:

Note In Visual Studio 2005, the default file is Program.cs. using Microsoft.Win32;   Add the following code to the Class1 class:

Note In Visual Studio 2005, add the code to the Program class. static void WriteRegistry(RegistryKey parentKey, String subKey, String valueName, Object value) {   RegistryKey key; try {       key = parentKey.OpenSubKey(subKey, true); if(key == null) //If the key doesn't exist. key = parentKey.CreateSubKey(subKey); //Set the value. key.SetValue(valueName, value);

Console.WriteLine(&quot;Value:{0} for {1} is successfully written.&quot;, value, valueName); }   catch(Exception e)    { Console.WriteLine(&quot;Error occurs in WriteRegistry&quot; + e.Message); } }

static void ReadRegistry(RegistryKey parentKey, String subKey, String valueName, Object value) {   RegistryKey key; try {       key = parentKey.OpenSubKey(subKey, true); if(key == null) //If the key doesn't exist. throw new Exception(&quot;The registry key doesn't exist&quot;);

//Get the value. value = key.GetValue(valueName);

Console.WriteLine(&quot;Value:{0} for {1} is successfully retrieved.&quot;, value, valueName); }   catch(Exception e)    { Console.WriteLine(&quot;Error occurs in ReadRegistry&quot; + e.Message); } }   Copy and paste the following code to replace the code in Main that is generated by Visual C#: static void Main(string[] args) {   WriteRegistry(Registry.CurrentUser, &quot;Software\\MySoftware&quot;, &quot;Count&quot;, 123);

Object value = null; ReadRegistry(Registry.CurrentUser, &quot;Software\\MySoftware&quot;, &quot;Count&quot;, value);

Console.ReadLine; } </li> Press F5 to run the application.

Note This procedure creates a registry key that is named MySoftware under the subkey HKEY_CURRENT_USER\Software. This procedure also creates a DWORD value named Count with the value 123 under the MySoftware key.</li></ol>

back to the top

Complete Code Listing
using System; using Microsoft.Win32;

class Class1 {   [STAThread] static void Main(string[] args) {       WriteRegistry(Registry.CurrentUser, &quot;Software\\MySoftware&quot;, &quot;Count&quot;, 123);

Object value = null; ReadRegistry(Registry.CurrentUser, &quot;Software\\MySoftware&quot;, &quot;Count&quot;, value);

Console.ReadLine; }

static void WriteRegistry(RegistryKey parentKey, String subKey, String valueName, Object value) {       RegistryKey key; try {           key = parentKey.OpenSubKey(subKey, true); if(key == null) //If the key doesn't exist. key = parentKey.CreateSubKey(subKey); //Set the value. key.SetValue(valueName, value);

Console.WriteLine(&quot;Value:{0} for {1} is successfully written.&quot;, value, valueName); }       catch(Exception e)        { Console.WriteLine(&quot;Error occurs in WriteRegistry&quot; + e.Message); }   }

static void ReadRegistry(RegistryKey parentKey, String subKey, String valueName, Object value) {       RegistryKey key;

try {           key = parentKey.OpenSubKey(subKey, true); if(key == null) //If the key doesn't exist. throw new Exception(&quot;The registry key doesn't exist&quot;);

//Get the value. value = key.GetValue(valueName);

Console.WriteLine(&quot;Value:{0} for {1} is successfully retrieved.&quot;, value, valueName); }       catch(Exception e)        { Console.WriteLine(&quot;Error occurs in ReadRegistry&quot; + e.Message); }   } } back to the top

<div class="references_section">