我一直在试图让我的应用程序在启动时运行。我添加了一个上下文菜单,可以打开和关闭此功能。上下文菜单在其左侧启用了“检查”功能(勾选时勾选)。让应用程序在启动时运行
//
// 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