Simple Registry Editor Class
Simple Registry Editor Class
a very simple class
to read, write, delete and count
registry values with C#
using System;
// its required for reading/writing into the registry:
using Microsoft.Win32;
// and for the MessageBox function:
using System.Windows.Forms;
namespace Utility.ModifyRegistry
{
/// <summary>
/// An useful class to read/write/delete/count registry keys
/// </summary>
public class ModifyRegistry
{
private bool showError = false;
/// <summary>
/// A property to show or hide error messages
/// (default = false)
/// </summary>
public bool ShowError
{
get { return showError; }
set { showError = value; }
}
private string subKey = "SOFTWARE" + Application.ProductName.ToUpper();
/// <summary>
/// A property to set the SubKey value
/// (default = ""SOFTWARE"" + Application.ProductName.ToUpper())
/// </summary>
public string SubKey
{
get { return subKey; }
set { subKey = value; }
}
private RegistryKey baseRegistryKey = Registry.LocalMachine;
/// <summary>
/// A property to set the BaseRegistryKey value.
/// (default = Registry.LocalMachine)
/// </summary>
public RegistryKey BaseRegistryKey
{
get { return baseRegistryKey; }
set { baseRegistryKey = value; }
}
/// <summary>
/// To read a registry key.
/// input: KeyName (string)
/// output: value (string)
/// </summary>
public string Read(string KeyName)
{
// Opening the registry key
RegistryKey rk = baseRegistryKey ;
// Open a subKey as read-only
RegistryKey sk1 = rk.OpenSubKey(subKey);
// If the RegistrySubKey doesnt exist -> (null)
if ( sk1 == null )
{
return null;
}
else
{
try
{
// If the RegistryKey exists I get its value
// or null is returned.
return (string)sk1.GetValue(KeyName.ToUpper());
}
catch (Exception e)
{
// AAAAAAAAAAARGH