如何在 Windows (8.1) 中将自定义应用程序注册为 Web 浏览器? [英] How do I register a custom application as a Web Browser in Windows (8.1)?

查看:46
本文介绍了如何在 Windows (8.1) 中将自定义应用程序注册为 Web 浏览器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试注册我自己的应用程序,以便使用我找到的信息在 Windows 中选择默认浏览器的列表中显示它周围 互联网.代码全部运行都没有问题,并且似乎创建了正确的注册表项,但我的应用程序没有显示在 Windows 8.1 的浏览器选择选项中.

I'm trying to register my own app so it appears in the list for selecting a a default browser in Windows using info I've found around the internet. The code all runs with no problem, and seems to create the correct registry keys, but my app doesn't show up in the Browser Selection options in Windows 8.1.

我没有设置一些在线代码示例中显示的 UserChoice 值,因为它看起来实际上设置了默认浏览器(只有一个值),我没有尝试要做到这一点,只需将其注册为一个选项.

I haven't set the UserChoice values shown in some code samples online, as that looks like it'd actually set the default browser (there's only one value), and I'm not trying to do that, only register it as an option.

相关代码在 RegisterBrowser 中,但为了方便起见,我包含了完整的类.

The relevant code is in RegisterBrowser, but I've included the full class for convenience.

using System;
using System.Reflection;
using Microsoft.Win32;

namespace MyApp
{
    class Program
    {
        const string AppID = "MyApp";
        const string AppName = "My App";
        const string AppDescription = "My App";
        static string AppPath = Assembly.GetExecutingAssembly().Location;
        static string AppIcon = AppPath + ",0";
        static string AppOpenUrlCommand = AppPath + " %1";
        static string AppReinstallCommand = AppPath + " --register";

        static void Main(string[] args)
        {
            if (args == null || args.Length != 1 || !HandleArg(args[0]))
                ShowHelpInfo();
        }

        static bool HandleArg(string arg)
        {
            if (string.Equals(arg, "--register", StringComparison.OrdinalIgnoreCase))
                RegisterBrowser();
            else if (string.Equals(arg, "--unregister", StringComparison.OrdinalIgnoreCase))
                UnregisterBrowser();
            else if (arg.StartsWith("http://", StringComparison.OrdinalIgnoreCase) || arg.StartsWith("https://", StringComparison.OrdinalIgnoreCase) || arg.StartsWith("ftp://", StringComparison.OrdinalIgnoreCase))
                LaunchBrowser(arg);
            else
                return false;

            return true;
        }

        static void ShowHelpInfo()
        {
            Console.WriteLine("Usage:");
            Console.WriteLine("    MyApp.exe --register               Register as web browser");
            Console.WriteLine("    MyApp.exe --unregister             Unregister as web browser");
            Console.WriteLine("    MyApp.exe \"http://example.org/\"  Launch example.org in specified browser");
        }

        static void RegisterBrowser()
        {
            // Register application.
            var appReg = Registry.LocalMachine.CreateSubKey(string.Format("SOFTWARE\\Clients\\StartMenuInternet\\{0}", AppID));
            appReg.SetValue("", AppName);
            appReg.CreateSubKey("DefaultIcon").SetValue("", AppIcon);
            appReg.CreateSubKey("shell\\open\\command").SetValue("", AppOpenUrlCommand);

            // Install info.
            var appInstallInfo = appReg.CreateSubKey("InstallInfo");
            appInstallInfo.SetValue("IconsVisible", 1);
            appInstallInfo.SetValue("ShowIconsCommand", AppPath); // TOOD: Do I need to support this?
            appInstallInfo.SetValue("HideIconsCommand", AppPath); // TOOD: Do I need to support this?
            appInstallInfo.SetValue("ReinstallCommand", AppReinstallCommand);

            // Register capabilities.
            var capabilityReg = appReg.CreateSubKey("Capabilities");
            capabilityReg.SetValue("ApplicationName", AppName);
            capabilityReg.SetValue("ApplicationIcon", AppIcon);
            capabilityReg.SetValue("ApplicationDescription", AppDescription);

            // Set up protocols we want to handle.
            var urlAssoc = capabilityReg.CreateSubKey("URLAssociations");
            urlAssoc.SetValue("http", AppID);
            urlAssoc.SetValue("https", AppID);
            urlAssoc.SetValue("ftp", AppID);
        }

        static void UnregisterBrowser()
        {
            Registry.LocalMachine.DeleteSubKeyTree(string.Format("SOFTWARE\\Clients\\StartMenuInternet\\{0}", AppID), false);
        }

        static void LaunchBrowser(string arg)
        {
            Console.WriteLine(arg);
            Console.ReadLine();
        }
    }
}

推荐答案

我不知道具体的代码,但如果您只需要一个注册表文件,您可以执行以下操作:(它将使用默认程序注册您的应用程序,并且为您设置所有处理程序)

I don't know about code specifically but if you just need a registry file you can do the following: (it will register your app with the default programs and set up all the handlers for you)

Windows Registry Editor Version 5.00

; Infamous capabilities:

[HKEY_LOCAL_MACHINE\SOFTWARE\MyApp\Capabilities]
"ApplicationDescription"="MyApp"
"ApplicationIcon"="C:\\MyApp\\MyApp.exe,0"
"ApplicationName"="MyApp"

[HKEY_LOCAL_MACHINE\SOFTWARE\MyApp\Capabilities\FileAssociations]
".htm"="MyAppURL"
".html"="MyAppURL"
".shtml"="MyAppURL"
".xht"="MyAppURL"
".xhtml"="MyAppURL"

[HKEY_LOCAL_MACHINE\SOFTWARE\MyApp\Capabilities\URLAssociations]
"ftp"="MyAppURL"
"http"="MyAppURL"
"https"="MyAppURL"

; Register to Default Programs

[HKEY_LOCAL_MACHINE\SOFTWARE\RegisteredApplications]
"MyApp"="Software\\MyApp\\Capabilities"

; MyAppURL HANDLER:

[HKEY_LOCAL_MACHINE\Software\Classes\MyAppURL]
@="MyApp Document"
"FriendlyTypeName"="MyApp Document"

[HKEY_LOCAL_MACHINE\Software\Classes\MyAppURL\shell]

[HKEY_LOCAL_MACHINE\Software\Classes\MyAppURL\shell\open]

[HKEY_LOCAL_MACHINE\Software\Classes\MyAppURL\shell\open\command]
@="\"C:\\MyApp\\MyApp.exe\" \"%1\""

这篇关于如何在 Windows (8.1) 中将自定义应用程序注册为 Web 浏览器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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