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

ここで何が間違っているのかわかりません。これにより、アプリの新しいレジストリエントリが設定されます。

コード行を追加してコンストラクターにレジストリエントリを作成すると、エントリは問題なく作成されます。

アイデア?

4

2 に答える 2

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);
            }               
        }
于 2013-02-10T03:39:43.973 に答える
0

コンストラクターでラムダ関数を作成すると、問題が解決しました。

    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);
        } 
    };
于 2013-02-10T09:08:41.433 に答える