using Microsoft.Win32; // for registry

/*
* This region used to control the Registry value.
*/

#region [ Registry Control ]
const string strUserRoot = "HKEY_CURRENT_USER\\SOFTWARE";

//
// set string value to registry
public void setRegistry(string strSubKey, string strName, object objValue)
{
// The name of the key must include a valid root.
string strKey = strUserRoot + "\\" + strSubKey;

try
{
Registry.SetValue(strKey, strName, objValue);
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}

//
// get interger value from registry
public int getRegInteger(string strSubKey, string strName)
{
// The name of the key must include a valid root.
string strKey = strUserRoot + "\\" + strSubKey;

try
{
return Convert.ToInt32(Registry.GetValue(strKey, strName, 10));
}
catch (NullReferenceException ex)
{
// set temp value
setRegistry(strKey, strName, false);
return Convert.ToInt32(Registry.GetValue(strKey, strName, null));
}

catch (Exception ex)
{
MessageBox.Show(ex.ToString());
return 0;
}
}

//
// get string value from registry
public string getRegString(string strSubKey, string strName)
{
// The name of the key must include a valid root.
string strKey = strUserRoot + "\\" + strSubKey;

try
{
return (string)Registry.GetValue(strKey, strName, null);
}
catch (NullReferenceException ex)
{
// if register key isn't in the Register, function will return NullReferenceException.
//
// 레지스터 안에 레지스터 키 값이 없다면 Registry.GetValue() 는 NullReferenceException 이 발생하며
// 이를 방지하기 위해 Register Key를 Registry.SetValue() 를 사용해 생성한 후 다시 Registry.GetValue()를 한다.
setRegistry(strKey, strName, false);

return (string)Registry.GetValue(strKey, strName, null);

}

catch (Exception ex)
{
MessageBox.Show(ex.ToString());
return null;
}
}
#endregion

사용
const string strSubKey = "ComOption";
private void getComOption()
{
// Call method from registry
int iMethod = (int)utility.getRegInteger(strSubKey, "method");
}

 

 

+ Recent posts