2016-02-25 9 views
-1
using System; 
using System.Windows.Forms; 
using Microsoft.Win32; 

namespace Aodb 
{ 
    internal static class AodbProtocol 
    { 
     private const string _Protocol = "aodb"; 
     private const string _ProtocolHandler = "url.aodb"; 

     private static readonly string _launch = string.Format(
      "{0}{1}{0} {0}%1{0}", (char)34, Application.ExecutablePath); 

     private static readonly Version _win8Version = new Version(6, 2, 9200, 0); 

     private static readonly bool _isWin8 = 
      Environment.OSVersion.Platform == PlatformID.Win32NT && 
      Environment.OSVersion.Version >= _win8Version; 

     internal static void Register() 
     { 
      if (_isWin8) RegisterWin8(); 
      else RegisterWin7(); 
     } 

     private static void RegisterWin7() 
     { 
      var regKey = Registry.ClassesRoot.CreateSubKey(_Protocol); 
      regKey.SetValue(null, "URL:aodb Protocol"); 
      regKey.SetValue("URL Protocol", ""); 

      regKey = regKey.CreateSubKey(@"shell\open\command"); 
      regKey.SetValue(null, _launch); 
     } 

     private static void RegisterWin8() 
     { 
      var regKey = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Classes") 
       .CreateSubKey(_ProtocolHandler); 

      regKey.SetValue(null, _Protocol); 

      regKey.CreateSubKey("DefaultIcon") 
       .SetValue(null, string.Format(
        "{0}{1},1{0}", "\"", Application.ExecutablePath)); 

      regKey.CreateSubKey(@"shell\open\command").SetValue(null, _launch); 

      Registry.LocalMachine.CreateSubKey(string.Format(
       @"SOFTWARE\{0}\{1}\Capabilities\ApplicationDescription\URLAssociations", 
       Application.CompanyName, Application.ProductName)) 
       .SetValue(_Protocol, _ProtocolHandler); 

      Registry.LocalMachine.CreateSubKey(@"SOFTWARE\RegisteredApplications") 
       .SetValue(Application.ProductName, string.Format(
        @"SOFTWARE\{0}\Capabilities", Application.ProductName)); 
     } 

     internal static void Unregister() 
     { 
      if (!_isWin8) 
      { 
       Registry.ClassesRoot.DeleteSubKeyTree("aodb", false); 
       return; 
      } 

      // extra work required. 
      Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Classes") 
       .DeleteSubKeyTree(_ProtocolHandler, false); 

      Registry.LocalMachine.DeleteSubKeyTree(string.Format(@"SOFTWARE\{0}\{1}", 
       Application.CompanyName, Application.ProductName)); 

      Registry.LocalMachine.CreateSubKey(@"SOFTWARE\RegisteredApplications") 
       .DeleteValue(Application.ProductName); 
     } 
    } 
} 

выше класс кусочкам из фрагментов кода, который я нашел в сети, а именно:Регистрация пользовательских URL Handler в C# на Windows 8

http://dotnet-snippets.com/snippet/register-user-defined-url-protocol/2707 < - работает на Win7, но не Win8

, который привел меня, чтобы найти Registering a protocol handler in Windows 8, что является неподтвержденным ответом.

ОДНАКО, я не могу заставить протокол URL работать в Win8; нажатие на aodb: // 1234 гиперссылка не запускает приложение, и веб-браузер жалуется, что протокол не поддерживается, и я считаю, что приведенная выше статья не является правильным ответом.

любой, кто знает о обработчиках протоколов, знаете, где я ошибся в приведенном выше коде и почему протокол не регистрируется в win8? Я вижу, что приведенный выше код работает, просматривая ключи реестра в regedit, но по какой-то причине протокол не распознается.

+0

Похоже, вы должны удалить эту строку 'if (! _isWin8) return;', запустите свое приложение, а затем перезапустите браузер, если он работает. – Sakura

+0

эта часть предназначена для версии Win7, ключ реестра разделяется вначале унарным оператором. – Xyphos

+0

в [link] (http://stackoverflow.com/questions/13559915/registering-a-protocol-handler-in-windows-8) вы публикуете, они записывают этот реестр, но вы не делаете: 'HKLM \ SOFTWARE \ MyApp \ Capabilities \ ApplicationDescription \ URLAssociations myprotocol = MyApp.ProtocolHandler // Связанный с вашим ProgID' – Sakura

ответ

1

Я ПОЛУЧИЛ ЭТО! Наконец. Ок, похоже, вам нужно реализовать как функции Win7, так и Win8 для Win8 и выше, но Win7 не требует дополнительного кода. ниже - последний класс для регистрации пользовательского протокола.

using System; 
using System.Windows.Forms; 
using Microsoft.Win32; 

namespace Aodb 
{ 
    internal static class AodbProtocol 
    { 
     private const string _Protocol = "aodb"; 
     private const string _ProtocolHandler = "url.aodb"; 

     private static readonly string _launch = string.Format(
      "{0}{1}{0} {0}%1{0}", (char)34, Application.ExecutablePath); 

     private static readonly Version _win8Version = new Version(6, 2, 9200, 0); 

     private static readonly bool _isWin8 = 
      Environment.OSVersion.Platform == PlatformID.Win32NT && 
      Environment.OSVersion.Version >= _win8Version; 

     internal static void Register() 
     { 
      if (_isWin8) RegisterWin8(); 
      else RegisterWin7(); 
     } 

     private static void RegisterWin7() 
     { 
      var regKey = Registry.ClassesRoot.CreateSubKey(_Protocol); 

      regKey.CreateSubKey("DefaultIcon") 
       .SetValue(null, string.Format("{0}{1},1{0}", (char)34, 
        Application.ExecutablePath)); 

      regKey.SetValue(null, "URL:aodb Protocol"); 
      regKey.SetValue("URL Protocol", ""); 

      regKey = regKey.CreateSubKey(@"shell\open\command"); 
      regKey.SetValue(null, _launch); 
     } 

     private static void RegisterWin8() 
     { 
      RegisterWin7(); 

      var regKey = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Classes") 
       .CreateSubKey(_ProtocolHandler); 

      regKey.SetValue(null, _Protocol); 

      regKey.CreateSubKey("DefaultIcon") 
       .SetValue(null, string.Format("{0}{1},1{0}", (char)34, 
        Application.ExecutablePath)); 

      regKey.CreateSubKey(@"shell\open\command").SetValue(null, _launch); 

      Registry.LocalMachine.CreateSubKey(string.Format(
       @"SOFTWARE\{0}\{1}\Capabilities\ApplicationDescription\URLAssociations", 
       Application.CompanyName, Application.ProductName)) 
       .SetValue(_Protocol, _ProtocolHandler); 

      Registry.LocalMachine.CreateSubKey(@"SOFTWARE\RegisteredApplications") 
       .SetValue(Application.ProductName, string.Format(
        @"SOFTWARE\{0}\Capabilities", Application.ProductName)); 
     } 

     internal static void Unregister() 
     { 
      if (!_isWin8) 
      { 
       Registry.ClassesRoot.DeleteSubKeyTree("aodb", false); 
       return; 
      } 

      // extra work required. 
      Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Classes") 
       .DeleteSubKeyTree(_ProtocolHandler, false); 

      Registry.LocalMachine.DeleteSubKeyTree(string.Format(@"SOFTWARE\{0}\{1}", 
       Application.CompanyName, Application.ProductName)); 

      Registry.LocalMachine.CreateSubKey(@"SOFTWARE\RegisteredApplications") 
       .DeleteValue(Application.ProductName); 
     } 
    } 
} 

 Смежные вопросы

  • Нет связанных вопросов^_^