在 Windows 8 C# 中启动时运行应用程序 [英] Run application on startup in Windows 8 C#

查看:53
本文介绍了在 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 上,此解决方案有效.

doesn't work on Windows 8. I don't have idea why because on Windows 7 and on Windows XP this solution works.

你能帮我吗?

推荐答案

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

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.

然后你改变了

致:

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

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

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

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:

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

这篇关于在 Windows 8 C# 中启动时运行应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆