gpt4 book ai didi

c# - 在 Windows 8 上的 C# 中注册自定义 URL 处理程序

转载 作者:行者123 更新时间:2023-12-02 17:29:20 29 4
gpt4 key购买 nike

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

这让我找到了 Registering a protocol handler in Windows 8这是一个未经证实的答案。

但是,我无法让 URL 协议(protocol)在 Win8 中工作;单击 aodb://1234 超链接不会启动应用程序,网络浏览器会提示该协议(protocol)不受支持,我认为上述文章不是正确的答案。

任何了解协议(protocol)处理程序的人,是否知道我在上面的代码中哪里出错了,为什么协议(protocol)没有在 win8 中注册?通过查看 regedit 中的注册表项,我可以看到上面的代码有效,但由于某种原因,无法识别该协议(protocol)。

最佳答案

我明白了!最后。好吧,看来Win8及以上版本必须同时实现Win7和Win8的注册表功能,不过Win7不需要额外的代码。下面是注册自定义协议(protocol)的最后一个类。

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

关于c# - 在 Windows 8 上的 C# 中注册自定义 URL 处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35626050/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com