【问题标题】:Run application on startup in Windows 8 C#在 Windows 8 C# 中启动时运行应用程序
【发布时间】:2014-03-18 18:11:38
【问题描述】:

这段代码:

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 上这个解决方案有效。

你能帮帮我吗?

【问题讨论】:

  • 您是否以管理员身份运行?
  • “它不起作用”相当模糊。需要详细说明吗?
  • 我以管理员身份运行。当我午餐计划时,他工作正常。但是重新启动操作系统后,他不会自动工作。我只在 Windows 8 上注意到这个问题,因为在重新启动 Windows 7 或 Windows XP 后,程序会自动运行。
  • 我尝试了 HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run 和 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run,但这没有帮助。每次,我都必须在 Windows 8 上手动应用午餐。

标签: c# windows-8 registry startup


【解决方案1】:

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

然后你改变

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

收件人:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-26
    • 1970-01-01
    • 2011-07-02
    • 2011-08-22
    • 2017-04-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多