Reading and Writing to the Registry


Introduction

This is a class that both reads and writes to the registry that I made while converting some applications to use C#.  This registry utility only reads and writes strings at the moment, but I'm sure if you wanted to read or write binary data you could modify the existing code to suit your needs.

One thing to note about my programming style (which most of my colleagues detest) is that my function will return a zero if successful.  I just find it easier to use constants to define errors and return them so I can be more accurate with my error handling.

Functions

The function WriteToReg takes an enum indicating the registry root to use.  The second argument is the path to the registry key that you want to write to.  The third parameter is the Key name, and the fourth the key value.  It should be noted that this function will create the registry key if it does not exist already.

The ReadFromRegistry function takes an enum indicating the registry root to use.  The second argument is the path to the registry key, the third is the name of the key from which you want to retrieve the value, and the fourth is a default value to return if the key does not exist.

Source Code

namespace RFG
{
using System;
using Microsoft.Win32;
public class RegUtil
{
public const int REG_ERR_NULL_REFERENCE = 12;
public const int REG_ERR_SECURITY = 13;
public const int REG_ERR_UNKNOWN = 14;
public enum REGUTIL_KEYS
{
HKCR = 0,
HKCU = 1,
HKLM = 2,
HKU = 3,
HKCC = 4
}
char[] Delimit = {'\x005c'}; //Hex for '\'
public RegUtil()
{}
public int WriteToReg(REGUTIL_KEYS Key, string RegPath, string KeyName, string KeyValue)
{
string[] RegString;
RegString = RegPath.Split(Delimit);
//First item of array will be the base key, so be carefull iterating below
RegistryKey[] RegKey = new RegistryKey[RegPath.Length + 1];
//Returns proper key -- Will Default to hkey_current_user
RegKey[0] = SelectKey(Key);
for(int i = 0;i < RegString.Length;i++)
{
RegKey[i + 1] = RegKey[i].OpenSubKey(RegString[i],
true);
//If key does not exist, create it. This logic usually suits my needs, but
//you may change it if you wish.
if (RegKey[i + 1] == null)
{
RegKey[i + 1] = RegKey[i].CreateSubKey(RegString[i]);
}
}
//Write the value to the registry. If fail, return the constant values defined at the beginning of the class
try
{
RegKey[RegString.Length].SetValue(KeyName, KeyValue);
}
catch (System.NullReferenceException)
{
return REG_ERR_NULL_REFERENCE;
}
catch (System.UnauthorizedAccessException)
{
return REG_ERR_SECURITY;
}
return 0;
}
//----------- End WriteToReg ----------------------------
///
<summary>
///
/// </summary>
/// <param name="Key"> </param>
/// <param name="RegPath"> </param>
/// <param name="KeyName"> </param>
/// <param name="DefaultValue"> </param>
public string ReadFromRegistry(REGUTIL_KEYS Key, string RegPath, string KeyName, string DefaultValue)
{
string[] RegString;
string Result = "";
RegString = RegPath.Split(Delimit);
//First item of array will be the base key, so be carefull iterating below
RegistryKey[] RegKey = new RegistryKey[RegPath.Length + 1];
//Returns proper key -- Will Default to HKEY_CURRENT_USER
RegKey[0] = SelectKey(Key);
for(int i = 0;i < RegString.Length;i++)
{
RegKey[i + 1] = RegKey[i].OpenSubKey(RegString[i]);
if (i == RegString.Length - 1 )
{
Result = (
string)RegKey[i + 1].GetValue(KeyName, DefaultValue);
}
}
return Result;
}
// -------------- End ReadFromRegistry -------------------
//Separated for cleanliness
private RegistryKey SelectKey(REGUTIL_KEYS Key)
{
switch (Key)
{
case REGUTIL_KEYS.HKCR:
return Registry.ClassesRoot;
case REGUTIL_KEYS.HKCU:
return Registry.CurrentUser;
case REGUTIL_KEYS.HKLM:
return Registry.LocalMachine;
case REGUTIL_KEYS.HKU:
return Registry.Users;
case REGUTIL_KEYS.HKCC:
return Registry.CurrentConfig;
default:
return Registry.CurrentUser;
}
}
//---------------- End SelectKey --------------------------
}// end Class
} // End Namespace


Similar Articles