2013-02-10 65 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