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



The information in this article applies to:

  • Microsoft Visual C# .NET (2003)
  • Microsoft Visual C# .NET (2002)
  • 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

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

MORE INFORMATION

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:
  1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
  2. On the File menu, point to New3, and then click Project.
  3. 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.
  4. Add the following namespace to Class1.cs:

    Note In Visual Studio 2005, the default file is Program.cs.
    using Microsoft.Win32;
  5. 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("Value:{0} for {1} is successfully written.", value, valueName);
    	}
    	catch(Exception e)
    	{
    		Console.WriteLine("Error occurs in WriteRegistry" + 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("The registry key doesn't exist");
    
    		//Get the value.
    		value = key.GetValue(valueName);
    
    		Console.WriteLine("Value:{0} for {1} is successfully retrieved.", value, valueName);
    	}
    	catch(Exception e)
    	{
    		Console.WriteLine("Error occurs in ReadRegistry" + e.Message);
    	}
    }
    
  6. 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, "Software\\MySoftware", "Count", 123);
    
    	Object value = null;
    	ReadRegistry(Registry.CurrentUser, "Software\\MySoftware", "Count", value);
    
    	Console.ReadLine();
    }
  7. 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.
back to the top

Complete Code Listing

using System;
using Microsoft.Win32;

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

		Object value = null;
		ReadRegistry(Registry.CurrentUser, "Software\\MySoftware", "Count", 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("Value:{0} for {1} is successfully written.", value, valueName);
		}
		catch(Exception e)
		{
			Console.WriteLine("Error occurs in WriteRegistry" + 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("The registry key doesn't exist");

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

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

Modification Type:MajorLast Reviewed:1/18/2006
Keywords:kbRegistry kbhowto KB816163 kbAudDeveloper