在 Windows 8 上的 C# 中注册自定义 URL 处理程序
本文关键字:自定义 URL 处理 程序 注册 Windows 上的 | 更新日期: 2023-09-27 18:36:06
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);
}
}
}
上面的类是从我在网上找到的代码片段拼凑而成的,具体来说:
http://dotnet-snippets.com/snippet/register-user-defined-url-protocol/2707 <-- 适用于 Win7,但不适用于 Win8
这导致我发现在 Windows 8 中注册协议处理程序,这是一个未经证实的答案。
但是,我无法让 URL 协议在 Win8 中工作;单击 aodb://1234 超链接不会启动应用程序,并且 Web 浏览器抱怨该协议不受支持,我相信上面的文章不是一个正确的答案。
有没有了解协议处理程序的人知道我在上面的代码中哪里出错了,为什么协议没有在 Win8 中注册?我可以通过查看注册表编辑器中的注册表项看到上面的代码,但由于某种原因,无法识别该协议。
我知道了!最后。好的,似乎您必须为 Win8 及更高版本实现 Win7 和 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);
}
}
}