使应用程序在启动时运行

时间:2013-02-10 03:28:42

标签: c# winforms events startup

我一直试图让我的应用在启动时运行。我添加了一个上下文菜单,可以打开和关闭此功能。上下文菜单在其左侧启用了“检查”功能(选中时选中了复选标记)。

    // 
    // 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);
                }
            }

我看不出我在这里做错了什么。这应该为我的应用程序设置一个新的注册表项。

如果我添加代码行来在构造函数中创建注册表项,它将创建条目就好了。

想法?

2 个答案:

答案 0 :(得分: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);
            }               
        }

答案 1 :(得分: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);
        } 
    };