2011-10-18 10 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

関連する問題