代码之家  ›  专栏  ›  技术社区  ›  Rytmis

我的应用程序如何在Windows7中注册为浏览器?

  •  1
  • Rytmis  · 技术社区  · 15 年前

    我已经编写了一个应用程序,它可以根据提供的URL启动浏览器--其思想是,我有一些网站,我想在IE中自动打开,而其余的应该在我的默认浏览器中打开。这个东西运行得很好,所以现在我尝试将它注册为浏览器,这样我就可以将它设置为默认值。我试着通过应用程序本身来实现。

    任务是写信给 HKLM\Software\Clients\StartMenuInternet (如文件所示) here ),但它一直在失败。我的应用程序使用UAC执行以下技巧将自己提升为管理员:

    private static void EnsureElevatedInstall()
    {
        using (var wi = WindowsIdentity.GetCurrent())
        {
            var wp = new WindowsPrincipal(wi);
    
            if (wp.IsInRole(WindowsBuiltInRole.Administrator)) return;
    
            using (var currentProc = Process.GetCurrentProcess())
            using (var proc = new Process())
            {
                proc.StartInfo.Verb = "runas";
                proc.StartInfo.FileName = currentProc.MainModule.FileName;
                proc.StartInfo.Arguments = "/reinstall";
                proc.Start();
            }
    
            Environment.Exit(0);
        }
    }
    

    这可以确保应用程序以管理员身份运行。我在启动进程时得到UAC提示。但是,当真的到了写注册表的时候,我得到了一个异常,说我没有必要的权限。

    using (var registryKey = 
        Registry.LocalMachine.OpenSubKey(@"Software\Clients\StartMenuInternet"))
    {
        const string launcherExecutable = "MyApp.exe";
    
        using(var launcherKey = registryKey.CreateSubKey(launcherExecutable))
        {
            // I never get here, because CreateSubKey fails
        }
    }
    

    有什么好处?

    1 回复  |  直到 15 年前
        1
  •  1
  •   RobertPitt    15 年前

    经过一些研究,我发现写权限 可以

    您的线路:

    ...OpenSubKey(@"Software\Clients\StartMenuInternet",true)...
    

    http://msdn.microsoft.com/en-us/library/xthy8s8d.aspx