【C#】 URL Protocol

【C#】 URL Protocol

网页调用本地程序, 支持 Windows 下所有浏览器, 与浏览器插件对比实现简单,但判断是否调用成功时, 只有ie10以上有函数,其他浏览器得自己实现(用 iframe)

实现 :

1. 写入注册表,格式如下  

Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\regName]  -- regName 自定义的注册表键名
"URL Protocol"=""
[HKEY_CLASSES_ROOT\regName\DefaultIcon]
@="icoPath"
[HKEY_CLASSES_ROOT\regName\shell]
[HKEY_CLASSES_ROOT\regName\shell\open]
[HKEY_CLASSES_ROOT\regName\shell\open\command]
@="filePath \"%1\"" --filePath 本地程序路径, %1 为参数写死即可

2. js 调用  

// 支持 ie 10 以上
function openForIe10(para) {
    var message = "regName:\\"+para;
    navigator.msLaunchUri(message,
              function () {
                  alert('success'); // 调用成功
              },
              function () {
                  alert('failed'); // 调用失败
              }
      );
}