简体   繁体   English

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

[英]Run application on startup in Windows 8 C#

This code: 这段代码:

RegistryKey rKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);

            rKey.DeleteValue(Application.ProductName, false);
            rKey.SetValue(Application.ProductName, Application.ExecutablePath, RegistryValueKind.String);

doesn't work on Windows 8. I don't have idea why because on Windows 7 and on Windows XP this solution works. 在Windows 8上不起作用。我不知道为什么,因为在Windows 7和Windows XP上这个解决方案有效。

Can you help me? 你能帮助我吗?

In order to set something in the registry you need to run the application as an administrator. 要在注册表中设置某些内容,您需要以管理员身份运行该应用程序。 To do so you first add a Application Manifest File to the Properties "folder" in the project. 为此,首先将Application Manifest File添加到项目的Properties“文件夹”中。

Then you change 然后你改变

<requestedExecutionLevel level="asInvoker" uiAccess="false" />

To: 至:

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

Then I don't know if the way you get the current executable path is correct, for me this have worked at least: 然后我不知道你获取当前可执行文件路径的方式是否正确,对我来说这至少起作用了:

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();
    }
}

Another note is that if the application is compiled in x86 and the OS is x64 the registry key will end up in the Wow64 registry which makes it the following path: 另一个注意事项是,如果应用程序是在x86中编译的,并且操作系统是x64,则注册表项将最终出现在Wow64注册表中,从而使其成为以下路径:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM