1

I have been trying to make my app run on startup. I have added a context menu which should switch this feature on and off. The context menu has the "check" feature enabled (gets a check mark when checked) on its left.

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

And this is what I did in 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);
                }
            }

I can't see what I am doing wrong here. This should set a new registry entry for my app.

If I add the lines of code to create the registry entry in the constructor, it will create the entry just fine.

Ideas?

aborted
  • 4,481
  • 14
  • 69
  • 132
  • Have you tried through project settings?Try to give a value there and check if it is equal to that value and load the settings. – coder Feb 10 '13 at 03:32
  • I have no idea how to do that. – aborted Feb 10 '13 at 03:34
  • Here is the link for that http://msdn.microsoft.com/en-us/library/vstudio/25zf0ze8(v=vs.100).aspx – coder Feb 10 '13 at 03:35
  • Isn't there anything wrong with my code and fixing it would solve the issue? I mean, I got something written that might be fixed (maybe), just abandoning the code and going to something else without trying to fix it doesn't seem like a solution to me. – aborted Feb 10 '13 at 03:37

2 Answers2

1

If you want the registry key created on application startup, you need to call the menu_startup_Click method from the constructor.

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);
            }               
        }
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • The change suggested by you simply creates the registry key, but doesn't: 1) Add the checkmark to the contextmenu 2) Remove the registry entry when the contextmenu is clicked again - Meaning that I am where I was in the beginning (I could create the registry entry from the constructor, mentioned in the question). **Edit:** With the line `this.menu_startup.Checked = false;` I tried to remove the checkmark if the registry entry does not exist. – aborted Feb 10 '13 at 03:51
  • Thank you. I tried your edit, but it still doesn't work. The only change is that the checkmark is there if the registry entry exists, meaning we are on the right track - I guess. – aborted Feb 10 '13 at 04:09
0

Making a lambda function in the constructor solved the issue.

    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);
        } 
    };
aborted
  • 4,481
  • 14
  • 69
  • 132