using System;
using Microsoft.Win32;
using System.Reflection;

public class RegistryAccess{

    private RegistryKey _Software;
    private RegistryKey _Company;
    private RegistryKey _Product;

    public RegistryAccess()
        :this(((AssemblyCompanyAttribute)Attribute.GetCustomAttribute(Assembly.GetExecutingAssembly()
                , typeof(AssemblyCompanyAttribute) 
                , true)).Company
            , ((AssemblyProductAttribute)Attribute.GetCustomAttribute(Assembly.GetExecutingAssembly()
                , typeof(AssemblyProductAttribute) 
                , true)).Product 
             )
    {
    }

    public RegistryAccess(string CompanyName
        , string ProductName){

        _Software = Registry.LocalMachine.OpenSubKey("Software");
        _Company = _Software.OpenSubKey(CompanyName);
        if(null == _Company)
            _Company = Registry.LocalMachine.CreateSubKey(
              "Software\\" + CompanyName);
        _Product = _Company.OpenSubKey(ProductName, true);
        if(null==_Product)
            _Product = Registry.LocalMachine.CreateSubKey(
				String.Format("Software\\{0}\\{1}"
                , CompanyName, ProductName));
    }

    public void WriteRegistryValue(string ValueName 
        , string Value){

        _Product.SetValue(ValueName, Value);

    }

    public string ReadRegistryValue(string ValueName){

        return (string)_Product.GetValue(ValueName);

    }

}
