我一直在試圖讓我的應用程序在啓動時運行。我添加了一個上下文菜單,可以打開和關閉此功能。上下文菜單在其左側啓用了「檢查」功能(勾選時勾選)。讓應用程序在啓動時運行
//
// 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);
}
}
做我看不到我在做什麼錯在這裏。這應該爲我的應用程序設置一個新的註冊表項。
如果我添加代碼行來在構造函數中創建註冊表項,它會創建條目就好了。
想法?
您是否嘗試過通過項目設置?嘗試給出一個值並檢查它是否等於該值並加載設置。 – coder 2013-02-10 03:32:59
我不知道該怎麼做。 – Aborted 2013-02-10 03:34:06
這是http://msdn.microsoft.com/en-us/library/vstudio/25zf0ze8(v=vs.100).aspx – coder 2013-02-10 03:35:28