起動時にアプリを実行しようとしています。この機能のオンとオフを切り替えるコンテキストメニューを追加しました。コンテキストメニューの左側で「チェック」機能が有効になっています(チェックするとチェックマークが付きます)。
//
// 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);
}
}
ここで何が間違っているのかわかりません。これにより、アプリの新しいレジストリエントリが設定されます。
コード行を追加してコンストラクターにレジストリエントリを作成すると、エントリは問題なく作成されます。
アイデア?