0

I am using the following code to manage the automatic startup of my application. The application has been set up to require admin privileges, and indeed, does ask for them.

RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
if (checkRunOnStartup.Checked)
{
    rkApp.SetValue("MyApp", Application.ExecutablePath.ToString());
}
else
{
    rkApp.DeleteValue("MyApp", false);
}

This does not work on any system I have tested on, except from my development machine. What am I doing wrong here?

4

1 回答 1

0

您可以在 Windows 启动文件夹中创建/删除应用程序的快捷方式,而不是编写 reg 键。Environment.SpecialFolder.Startup 将返回该路径。如果您在 reg 键选项上死心塌地,这里是每个DotnetThoughts的代码片段。似乎主要区别在于他放弃了 .ToString()。

private void RegisterInStartup(bool isChecked)
{
   RegistryKey registryKey = Registry.CurrentUser.OpenSubKey
        ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
   if (isChecked)
   {
       registryKey.SetValue("ApplicationName", Application.ExecutablePath);
   }
   else
   {
     registryKey.DeleteValue("ApplicationName");
   }
}
于 2013-04-25T14:04:56.230 回答