前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >WPF使用URL协议实现网页中打开应用

WPF使用URL协议实现网页中打开应用

作者头像
码客说
发布2022-10-28 15:18:13
1.1K0
发布2022-10-28 15:18:13
举报
文章被收录于专栏:码客

常见方案

网页唤起指定软件,其实就是利用URL来执行一个关键字Key,这个Key是注册表中的一个键,Value是指定路径的exe,亦可携带参数启动exe;

步骤1

检查关键字是否已存在

代码语言:javascript
复制
//检查注册表是否已包含 key
private static bool IsRegisteredKey(string key)
{
  var executablePath = string.Empty;
  try
  {
    var registryRunKey = Registry.ClassesRoot.OpenSubKey(key);
    if (registryRunKey != null)
    {
      executablePath = registryRunKey.GetValue("URL Protocol") as string;
      registryRunKey.Close();
    }
  }
  catch (Exception ex)
  {
    Logger.WriteLineError($"Get application {key} value failed, ex:{ex}");
  }

  Logger.WriteLineDebug($"AuoStartup executablePath - {executablePath}");

  return !string.IsNullOrEmpty(executablePath) && executablePath == AppExecutePath;
}

步骤2

注册关键字(步骤1、2通常在软件安装后或软件启动时执行)

代码语言:javascript
复制
private static bool RegisterAppKey(string keyName, string value)
{
  try
  {
    RegistryKey registryKey = Registry.ClassesRoot;
    var fKey = registryKey.OpenSubKey(keyName, true) ?? registryKey.CreateSubKey(keyName);
    fKey .SetValue("", value);
    fKey .SetValue("", keyName);
    if (flyinsonoKey != null)
    {
      var defaultIconKey = fKey .OpenSubKey("DefaultIcon", true) ?? flyinsonoKey.CreateSubKey("DefaultIcon");
      if (defaultIconKey != null)
      {
        defaultIconKey.SetValue("", value + ",1");
      }
      var shellKey = fKey .OpenSubKey("shell", true) ?? fKey .CreateSubKey("shell");
      var openKey = shellKey.OpenSubKey("open", true) ?? shellKey.CreateSubKey("open");
      var commandKey = openKey.OpenSubKey("command", true) ?? openKey.CreateSubKey("command");
      if (commandKey != null)
      {
        commandKey.SetValue("", "\"" + value + "\"" + " \"%1\"");
      }
      fKey .SetValue("URL Protocol", value);
      fKey .Close();
    }

    return true;
  }
  catch (Exception ex)
  {
    Console.WriteLine($"Register ex:{ex}");
    return false;
  }
}

步骤3

网页中用Key写一个链接

代码语言:javascript
复制
<a href="MyApp://?a=arg1&e=arg2">点击打开MyApp.exe</a>

步骤4

软件启动时解析参数

代码语言:javascript
复制
//此处会获取到步骤2中设置的Value;和步骤3中的href;参数自行解析
var args = Environment.GetCommandLineArgs();

REG

保存为Notepad2.reg

代码语言:javascript
复制
Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Notepad2]
"URL Protocol"="D:\\Tools\\Notepad2\\Notepad2.exe"
@="Notepad2Protocol"

[HKEY_CLASSES_ROOT\Notepad2\DefaultIcon]
@="D:\\Tools\\Notepad2\\Notepad2.exe,1"

[HKEY_CLASSES_ROOT\Notepad2\shell]

[HKEY_CLASSES_ROOT\Notepad2\shell\open]

[HKEY_CLASSES_ROOT\Notepad2\shell\open\command]
@="\"D:\\Tools\\Notepad2\\Notepad2.exe\" \"%1\""

注意事项: 路径使用双杠\\ 如果字符串中有双引号(”),那么需要加转义字符”” 保存后双击文件执行,将这些项写入到注册表

检验是否注册成功:

开始-运行 输入Notepad2:,可以运行该程序则表示注册成功了; 在浏览器的地址栏直接输入:Notepad2:,可以运行则表示注册成功;

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-10-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 常见方案
    • 步骤1
      • 步骤2
        • 步骤3
          • 步骤4
          • REG
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档