C#

레지스트리(등록,추가삭제)

캡틴노랑이 2015. 8. 19. 09:57
반응형


public class RegeditHelper
{
   
    private const string strRegSubKey = @""Software\ECountSoftware"";

    public static void SetRegistry(string key, string value)
    {
        RegistryKey rk = null;
        try
        {
            rk = Registry.LocalMachine.OpenSubKey(strRegSubKey, true);

            if (rk == null)
            {
                //해당 이름으로 서브키를 생성한다.
                rk = Registry.LocalMachine.CreateSubKey(strRegSubKey);
            }
            //서브키 아래로 쓰기
            rk.SetValue(key, value);

        }
        catch (Exception ex)
        {
            // throw ex;
        }
        finally
        {
            if (rk != null)
                rk.Close(); rk = null;

        }
    }

    /// <summary>
    /// 레지스트에서 값을 가져온다.
    /// </summary>
    /// <param name=""key""></param>
    /// <returns></returns>
    public static string GetRegistry(string key)
    {
        string strValue = string.Empty;
        RegistryKey rk = null;
        try
        {
            rk = Registry.LocalMachine.OpenSubKey(strRegSubKey, true);
            strValue = rk.GetValue(key) as string;
            return strValue;
        }
        catch (Exception ex)
        {
            // throw ex;
            return """";
        }
        finally
        {
            if (rk != null)
                rk.Close(); rk = null;

        }
    }

    /// <summary>
    /// 레지스트의 서브키값을 삭제한다.
    /// </summary>
    /// <param name=""key""></param>
    public static void DelRegistry(string key)
    {
        try
        {
            Registry.LocalMachine.DeleteSubKey(strRegSubKey, true);
        }
        catch (Exception ex)
        {
        }
    }
}

반응형