2

我写了一个应用程序,它应该在 Windows 启动时启动。我在 HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Run 的 windows 寄存器中添加了一个条目。条目已成功添加,但程序未正常启动。

我已经在 Windows 7 64 位上测试了应用程序。应用程序需要具有管理员权限才能运行,也许这就是它无法启动的原因?

我还看到条目的值不在引号中,但其他值在引号中。是强制性的吗?

这是我的 C# 代码:

            var registry = Registry.CurrentUser;
            var key = registry.OpenSubKey(runKeyBase, true);
            key.SetValue(KEY, directory + @"\" + filename);
            Registry.CurrentUser.Flush();

我怎么不能让它工作?

4

3 回答 3

5

为什么不在启动文件夹中放置一个快捷方式?这样,您还可以将快捷方式的属性设置为以管理员身份运行

编辑:

导航到您要在启动时运行的 exe,然后右键单击,创建快捷方式。

在该快捷方式的属性中,选中以管理员身份运行。

然后将其放在启动文件夹中(您可以通过单击开始菜单中文件夹上的探索来到达那里)。这将在 Windows 登录时启动该应用程序。如果 UAC 需要批准,它将提示用户是否可以运行该程序。

于 2012-02-28T18:19:20.477 回答
3

据我所知,这是由于用户访问控制设置只允许签名的应用程序启动,否则它将要求管理员权限。

因此,在启动过程中,即使您完成了注册表设置,操作系统也不会运行该应用程序。

报价也不是强制性的。您可以拥有或不拥有它们。

我所做的方法是在 Startup 文件夹中放置一个快捷方式。注册表设置将不起作用。

此外,您可以尝试的一件事是将文件放在 /system32 或 /windows 中,然后尝试在注册表中进行设置。

于 2012-02-28T18:38:01.710 回答
0

您可以在启动时自行提升程序。只需在开头执行以下代码:

public static void runAsAdmin(string[] args)
    {
        ProcessStartInfo proc = new ProcessStartInfo();

        if (args != null)
            proc.Arguments = string.Concat(args);

        proc.UseShellExecute = true;
        proc.WorkingDirectory = Environment.CurrentDirectory;
        proc.FileName = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
        proc.Verb = "runas";



        bool isElevated;
        WindowsIdentity identity = WindowsIdentity.GetCurrent();
        WindowsPrincipal principal = new WindowsPrincipal(identity);
        isElevated = principal.IsInRole(WindowsBuiltInRole.Administrator);

        if (!isElevated)
        {

            try
            {
                Process.Start(proc);
            }
            catch
            {
                //No Admin rights, continue without them
                return;
            }
            //Close current process for switching to elevated one
            Environment.Exit(0);
        }
        return;
    }

此外,在获得管理员权限后,您可以禁用 UAC 通知(如果已启用)以在未来静默启动:

private void disableUAC()
    {
        RegistryKey regKey = null;

        try
        {
            regKey = Registry.LocalMachine.OpenSubKey(ControlServiceResources.UAC_REG_KEY, true);
        }

        catch (Exception e)
        {
            //Error accessing registry
        }


        try
        {
            regKey.SetValue("ConsentPromptBehaviorAdmin", 0);
        }
        catch (Exception e)
        {
            //Error during Promt disabling
        }


    }
于 2015-10-08T00:58:51.680 回答