如何在Windows(8.1)中将自定义应用程序注册为Web浏览器?

我试图注册我自己的应用程序,所以它出现在列表中select使用我在 互联网上find的信息在Windows中的默认浏览器。 代码都运行没有问题,似乎创build正确的registry项,但我的应用程序不显示在Windows 8.1中的浏览器select选项。

我没有设置一些在线代码示例中显示的UserChoice值,因为它看起来像它实际上设置的默认浏览器(只有一个值),我不想这样做,只注册它作为一个选项。

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

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

我不知道代码具体,但如果你只需要一个注册表文件,你可以做到以下几点:(它会注册您的应用程序的默认程序,并为您设置所有的处理程序)

 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\""