网页链接启动本地应用

参考:http://www.codeproject.com/Articles/15634/Custom-Hyperlinks-Using-Asynchronous-Pluggable-Pro

http://msdn.microsoft.com/en-us/library/aa767914.aspx

用代码直接写注册表会带来权限不足的问题。

所以写了CustomHlink这样类,自动生成.reg文件,执行.reg文件,完成注册表的注册。

然后写个网页

<a href="regName:Hello!">点击这里启动程序</a>

测试即可。


    // 用途:从网页超链接打开本地app
    class CustomHlink
    {
        public CustomHlink(string appPath, string appExeName)
        {
            string regName = appExeName.Remove(appExeName.Length - 4);
            regFileName = regName + ".reg";
            StreamWriter sw = new StreamWriter(regName + ".reg", false);
            sw.WriteLine("Windows Registry Editor Version 5.00");
            sw.WriteLine(@"[HKEY_CLASSES_ROOT\" + regName + "]");
            sw.WriteLine("@=\"URL:" + regName + " Protocol\"");
            sw.WriteLine("\"URL Protocol\"=\"\"");
            sw.WriteLine();
            sw.WriteLine(@"[HKEY_CLASSES_ROOT\" + regName + @"\DefaultIcon]");
            sw.WriteLine("@=\"" + appPath + appExeName + "\"");
            sw.WriteLine();
            sw.WriteLine(@"[HKEY_CLASSES_ROOT\" + regName + @"\shell]");
            sw.WriteLine(@"[HKEY_CLASSES_ROOT\" + regName + @"\shell\open]");
            sw.WriteLine(@"[HKEY_CLASSES_ROOT\" + regName + @"\shell\open\command]");
            sw.WriteLine("@=\"" + appPath + appExeName + "  %1\"");


            sw.Flush();
            sw.Close();

        }


        public string regFileName = "";


        public void runRegFile()
        {
            Process process = new Process();
            process.StartInfo.FileName = regFileName;
            process.StartInfo.WorkingDirectory = Environment.CurrentDirectory;
            process.Start();
        }
    
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值