본문으로 바로가기

윈도우 시작프로그램 등록 및 삭제

category Development/C# 2012. 2. 9. 18:04

/// /// 시작프로그램에 등록(레지스트리)
/// 
/// 등록 또는 제거 하고싶은 응용프로그램 이름
/// true(등록), false(삭제)
private void SetStartUp(string appName, bool enable)
{
    string runKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
    Microsoft.Win32.RegistryKey startupKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(runKey);

    if (enable)
    {
        if (startupKey.GetValue(appName) == null)
        {
            //add startup reg key
            startupKey.Close();
            startupKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(runKey, true);
            startupKey.SetValue(appName, Application.ExecutablePath.Tostring());
            startupKey.Close();
        }
    }
    else
    {
        //remove startup
        startupKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(runKey, true);
        startupKey.DeleteValue(appName, false);
        startupKey.Close();
    }
}

/// 
/// 시작프로그램 등록 체크
/// 
/// 응용프로그램 이름
/// true(등록), false(삭제)
private bool CheckStartUp(string appName)
{
    string runKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
    Microsoft.Win32.RegistryKey startupKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(runKey);

    if (startupKey.GetValue(appName) == null)
        return false;
    else
        return true;
}