在Windows 8 C中启动时运行应用程序#

这段代码:

RegistryKey rKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); rKey.DeleteValue(Application.ProductName, false); rKey.SetValue(Application.ProductName, Application.ExecutablePath, RegistryValueKind.String); 

在Windows 8上不起作用。我不知道为什么,因为在Windows 7和Windows XP上这个解决方案有效。

你能帮助我吗?

要在注册表中设置某些内容,您需要以管理员身份运行该应用程序。 为此,首先将Application Manifest File添加到项目的Properties“文件夹”中。

然后你改变

至:

然后我不知道你获取当前可执行文件路径的方式是否正确,对我来说这至少有用:

 class Program { private static void RegisterAsRun() { string exePath = new Uri(Assembly.GetEntryAssembly().CodeBase).LocalPath; Registry.SetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", "TestApp", exePath, RegistryValueKind.String); } static void Main(string[] args) { RegisterAsRun(); Console.WriteLine("Hello!"); Console.ReadLine(); } } 

另一个注意事项是,如果应用程序是在x86中编译的,并且操作系统是x64,则注册表项将最终位于Wow64注册表中,从而使其成为以下路径:

HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Run