2013-02-10 66 views
1

我一直在試圖讓我的應用程序在啓動時運行。我添加了一個上下文菜單,可以打開和關閉此功能。上下文菜單在其左側啓用了「檢查」功能(勾選時勾選)。讓應用程序在啓動時運行

// 
    // menu_startup 
    // 
    this.menu_startup.Name = "menu_startup"; 
    this.menu_startup.ShortcutKeyDisplayString = ""; 
    this.menu_startup.Size = new System.Drawing.Size(201, 22); 
    this.menu_startup.Text = "Run on startup"; 
    this.menu_startup.Click += new System.EventHandler(this.menu_startup_Click); 

這是我在Form1.cs

public string regKey = "IMGit"; 

     public Form1() 
     { 
      InitializeComponent(); 
      notifyIcon1.ContextMenuStrip = contextMenuStrip1; 

      RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); 

      if (rkApp.GetValue(this.regKey) == null) 
      { 
       this.menu_startup.Checked = false; 
      } 
      else 
      { 
       this.menu_startup.Checked = true; 
      } 

      this.menu_about.Click += menu_about_Click; // Ignore 
      this.menu_exit.Click += menu_exit_Click; // Ignore 
      this.menu_startup.Click += menu_startup_Click; 
     }  

      private void menu_startup_Click(object sender, EventArgs e) 
      { 
       RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); 

       if (rkApp.GetValue(this.regKey) == null) 
       { 
        rkApp.SetValue(this.regKey, Application.ExecutablePath.ToString()); 
       } 
       else 
       { 
        rkApp.DeleteValue(this.regKey, false); 
       } 
      } 

做我看不到我在做什麼錯在這裏。這應該爲我的應用程序設置一個新的註冊表項。

如果我添加代碼行來在構造函數中創建註冊表項,它會創建條目就好了。

想法?

+0

您是否嘗試過通過項目設置?嘗試給出一個值並檢查它是否等於該值並加載設置。 – coder 2013-02-10 03:32:59

+0

我不知道該怎麼做。 – Aborted 2013-02-10 03:34:06

+0

這是http://msdn.microsoft.com/en-us/library/vstudio/25zf0ze8(v=vs.100).aspx – coder 2013-02-10 03:35:28

回答

0

在構造函數中做一個lambda函數解決了這個問題。

this.menu_startup.Click += (s, ea) => 
    { 
     RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); 
     string appPath = Application.ExecutablePath.ToString(); 

     this.menu_startup.Checked = (rkApp.GetValue(this.regKey) == null); 

     if (rkApp.GetValue(this.regKey) == null) 
     { 
      rkApp.SetValue(this.regKey, appPath); 
     } 
     else 
     { 
      rkApp.DeleteValue(this.regKey, false); 
     } 
    }; 
1

如果您希望在應用程序啓動時創建註冊表項,則需要從構造函數中調用menu_startup_Click方法。

public Form1() 
     { 
      InitializeComponent(); 
      notifyIcon1.ContextMenuStrip = contextMenuStrip1; 

      //Make the call to add the registry key here (plus Check or Uncheck the menu) 
      menu_startup_Click(null,null); 

      this.menu_startup.Click += menu_startup_Click;     
     }  


private void menu_startup_Click(object sender, EventArgs e) 
     { 
      RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); 

      //Check or Uncheck the menu 
      this.menu_startup.Checked = (rkApp.GetValue(this.regKey) == null) 

      if (rkApp.GetValue(this.regKey) == null) 
      { 
       rkApp.SetValue(this.regKey, Application.ExecutablePath.ToString()); 
      } 
      else 
      { 
       rkApp.DeleteValue(this.regKey, false); 
      }    
     } 
+0

您建議的更改只是創建註冊表項,但不會:1)將複選標記添加到上下文菜單2)當再次單擊上下文菜單時刪除註冊表項 - 意味着我處於開始的位置(I可以從構造函數創建註冊表項,在問題中提到)。 **編輯:**使用'this.menu_startup.Checked = false'這行''如果註冊表項不存在,我嘗試刪除複選標記。 – Aborted 2013-02-10 03:51:02

+1

你能看看我的編輯,讓我知道它是否適合? – 2013-02-10 04:05:48

+0

謝謝。我試過了你的編輯,但它仍然不起作用。唯一的變化是,如果註冊表項存在,則檢查標記就在那裏,這意味着我們正處於正確的軌道 - 我猜。 – Aborted 2013-02-10 04:09:28