2011-10-18 7 views
0

컴퓨터 이름, OS 버전 및 로그인 한 사용자를 표시하는 (WindowStartup.EXE)라는 샘플 응용 프로그램을 만들었습니다.이 응용 프로그램은 컴퓨터를 시작할 때 자동 실행 동작으로 구성됩니다. 아래 코드를 참조하십시오. 코드가 설치 프로그램을 작성하고 내 컴퓨터에 설치 한 후 C#을내 컴퓨터에서 자동 실행을 제거하는 방법

private void InfoWindow_Load(object sender, EventArgs e) 
{ 
    lblMachineName.Text = Environment.MachineName.ToString(); 
    lblOSVersion.Text = Environment.OSVersion.ToString(); 
    lblUserlogged.Text = Environment.UserName.ToString(); 
    this.Top = Screen.PrimaryScreen.WorkingArea.Bottom - this.Height; 
    this.Left = Screen.PrimaryScreen.WorkingArea.Right - this.Width; 

    if (StartUp()) StartUpSystem(); 
} 

private bool StartUp() 
{ 
    bool retVal = false; 
    if (File.Exists(Application.StartupPath + "\\SystemFile.txt")) 
    { 
     //read text file if content is true 
     Stream file = new FileStream(Application.StartupPath + "\\SystemFile.txt", FileMode.Open, FileAccess.Read); 
     StreamReader reader = new StreamReader(file); 
     string content = reader.ReadToEnd(); 
     if (content == "true") retVal = true; 
    } 
    return retVal; 
} 

private void StartUpSystem() 
{ 
    RegistryKey regApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); 
    if (IsStartupItem()) 
    { 
     //--> Add the value in the registry so that the application runs at startup 
     regApp.SetValue("WindowStartup.EXE", Application.ExecutablePath.ToString()); 

    } 
} 

private bool IsStartupItem() 
{ 
    // The path to the key where Windows looks for startup applications 
    RegistryKey regApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); 

    if (regApp.GetValue("WindowStartup.EXE") == null) 
     // The value doesn't exist, the application is not set to run at startup 
     return false; 
    else 
     // The value exists, the application is set to run at startup 
     return true; 
} 

에 기록되고, 그것은 오류없이 실행됩니다. 그러나이 샘플 응용 프로그램을 제거하면 컴퓨터를 시작할 때마다 계속 팝업됩니다. 이 사람이 내가 그것을 프로그램 제거하는 방법에 도와 줄 수

private void StartUpSystem() 
{ 
    RegistryKey regApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); 
    if (!IsStartupItem()) 
    { 
     //--> Remove the value from the registry so that the application doesn't start 
     regApp.DeleteValue("WindowStartup.EXE", false); 

    } 
} 

를 작동하지 않는 것을

나는 레지스트리 값을 제거하려면 아래 코드를 시도 할 것이다 그러나 그것은 보인다?

답변

1
private void DeleteRegistryKey() 
    { 
     using (RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true)) 
     { 
      if (null != key && IsStartupItem()) 
      { 
       key.DeleteValue("MyApp"); 
      } 
     } 
    } 

    private bool IsStartupItem() 
    { 
     // The path to the key where Windows looks for startup applications 
     RegistryKey regApp = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true); 

     if (regApp.GetValue("MyApp") == null) 
      // The value doesn't exist, the application is not set to run at startup 
      return false; 
     else 
      // The value exists, the application is set to run at startup 
      return true; 
    } 

    private static void SetRegistry(string path) 
    { 
     if (!IsStartupItem()) 
     { 
      Registry.SetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run", "MyApp", path); 
     } 
    } 
+0

이미하지만 여전히 작동하지 그것을 발견? 추가 코드입니까 대체 코드입니까? – Bryan

+0

레지스트리 값을 설정할 때 "HKEY_CURRENT_USER \ Software \ ..."와 같은 전체 경로를 지정할 때 내 응답 – Damith

+1

을 업데이트하십시오. – Damith

1

는이되지 않을까 :

if(IsStartupItem()) //rather than !IsStartupItem() ? 
+0

그래, 나는 ... 나는이 코드를 삽입해야 – Bryan