[英]C# uri protocol
我正在用 c# (ASP WinForm) 開發兩個應用程序
我需要將一些參數從網絡發送到 WinForm 應用程序。 為此,我編寫了一個函數,允許我的應用程序為此連接創建一個 URI 協議:
public static void RegisterURLProtocol(string protocolName, string applicationPath, string description)
{
RegistryKey myKey = Registry.ClassesRoot.CreateSubKey(protocolName);
myKey.SetValue(null, description);
myKey.SetValue("URL Protocol", string.Empty);
Registry.ClassesRoot.CreateSubKey(protocolName + "\\Shell");
Registry.ClassesRoot.CreateSubKey(protocolName + "\\Shell\\open");
myKey = Registry.ClassesRoot.CreateSubKey(protocolName + "\\Shell\\open\\command");
myKey.SetValue(null, "\"" + applicationPath+ "\" %1");
}
我使用這個和平的代碼來調用函數:
RegisterURLProtocol("mAPP", Application.ExecutablePath, "mAPP Uri Protocol");
在 ASP 項目中,我將參數發送到我的應用程序,如下所示:
protected void Page_Load(object sender, EventArgs e)
{
Response.Redirect("mAPP://MYPARAMETERS");
}
但是當我嘗試像這樣打開我的 ASP 頁面時沒有任何反應:
http://mydomain/BlankPage.aspx
我怎樣才能解決這個問題?
從 Windos 8 開始,您將不得不添加更多注冊表項:
Registry.SetValue(
$@"HKEY_CLASSES_ROOT\{protocolName}",
string.Empty,
protocolValue,
RegistryValueKind.String);
Registry.SetValue(
$@"HKEY_CLASSES_ROOT\{protocolName}",
"URL Protocol",
String.Empty,
RegistryValueKind.String);
Registry.SetValue($@"HKEY_CLASSES_ROOT\{protocolName}\shell\open\command", string.Empty, command, RegistryValueKind.String);
// detect win 8 and register as choosable protocol handler
Version win8Version = new Version(6, 2, 9200, 0);
if (Environment.OSVersion.Platform == PlatformID.Win32NT &&
Environment.OSVersion.Version >= win8Version)
{
Registry.SetValue(
$@"HKEY_LOCAL_MACHINE\SOFTWARE\Classes\{protocolName}",
string.Empty,
protocolValue,
RegistryValueKind.String);
Registry.SetValue(
$@"HKEY_LOCAL_MACHINE\SOFTWARE\Classes\{protocolName}\shell\open\command",
string.Empty,
command,
RegistryValueKind.String);
Registry.SetValue(
$@"HKEY_LOCAL_MACHINE\SOFTWARE\{protocolName}\Capabilities\URLAssociations",
protocolName,
protocolName,
RegistryValueKind.String);
Registry.SetValue(
@"HKEY_LOCAL_MACHINE\SOFTWARE\RegisteredApplications",
protocolName,
$@"SOFTWARE\{protocolName}\Capabilities",
RegistryValueKind.String);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.