参考: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();
}
}