http://blog.csdn.net/shisuizhe/article/details/54949431?locationNum=6&fps=1
本篇主要实现的是在BS网页中使用a标签的href链接启动本地安装的CS应用程序(winform C#.exe程序),并能从网页传入参数到应用程序中。实现方式是利用自定义URL Protocol来调用应用程序,在winform程序安装完成后修改注册表,用注册表来启动应用程序。
网上也找了很多资料,主要结合了以下两位的技术分享
http://www.cnblogs.com/wang726zq/archive/2012/12/11/UrlProtocol.html
http://www.cnblogs.com/armyfai/p/5902482.html
下面我做了自己的整理:
winform的打包这里不展开,百度下很多。
1、首页创建一个类库项目,添加一个安装程序类,往里添加两个事件,然后往两个事件中编写修改注册表的代码
this.AfterInstall += new InstallEventHandler(InstallerDemo_AfterInstall);
this.AfterUninstall += new InstallEventHandler(InstallerDemo_AfterUninstall);
如下图所示:
![这里写图片描述](https://img-blog.csdn.net/20170209172025492?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc2hpc3Vpemhl/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
整体代码如下:
[RunInstaller(true)]
public partial class InstallerDemo : System.Configuration.Install.Installer
{
public InstallerDemo()
{
InitializeComponent();
this.AfterInstall += new InstallEventHandler(InstallerDemo_AfterInstall);
this.AfterUninstall += new InstallEventHandler(InstallerDemo_AfterUninstall);
}
private string targetdirPath = "";
void InstallerDemo_AfterInstall(object sender, InstallEventArgs e)
{
targetdirPath = this.Context.Parameters["targetdir"].TrimEnd('\\');
Msg("注册表信息添加开始:");
Msg("文件路径:" + Path.Combine(targetdirPath, "mes_wf_test.exe"));
var result= RegeditAdd("tpswftest", Path.Combine(targetdirPath, "mes_wf_test.exe"), "");
Msg("注册表修改是否成功:" + result);
}
void InstallerDemo_AfterUninstall(object sender, InstallEventArgs e)
{
Msg("注册表信息删除开始:");
var result= RegeditDelete("tpswftest");
Msg("注册表信息删除:"+result);
}
public bool RegeditAdd(string Root_Key, string file_application_path, string file_application_ico)
{
RegistryKey reg_ClassRoot = Registry.ClassesRoot;
try
{
RegistryKey reg_key = reg_ClassRoot.OpenSubKey(Root_Key, true);
if (reg_key == null)
{
RegistryKey reg_sjbs = reg_ClassRoot.CreateSubKey(Root_Key);
reg_sjbs.SetValue("", "URL: " + Root_Key + " Protocol Handler");
reg_sjbs.SetValue("URL Protocol", file_application_path);
RegistryKey reg_DefaultIcon = reg_sjbs.CreateSubKey("DefaultIcon");
if (!String.IsNullOrEmpty(file_application_ico))
{
reg_DefaultIcon.SetValue("", file_application_ico);
}
else
{
reg_DefaultIcon.SetValue("", file_application_path + ",1");
}
RegistryKey reg_command = reg_sjbs.CreateSubKey("shell").CreateSubKey("open").CreateSubKey("command");
reg_command.SetValue("", "\"" + file_application_path + "\" \"%1\"");
}
return true;
}
catch(Exception ex) {
Msg(ex.Message.ToString());
return false; }
finally { reg_ClassRoot.Close(); }
}
public bool RegeditDelete(string Root_Key)
{
RegistryKey reg_ClassRoot = Registry.ClassesRoot;
try
{
RegistryKey reg_sjbs = reg_ClassRoot.OpenSubKey(Root_Key, true);
if (reg_sjbs != null)
{
reg_ClassRoot.DeleteSubKeyTree(Root_Key);
return true;
}
else {
Msg("注册表中" + reg_ClassRoot + "\\" + Root_Key + "不存在!");
return false;
}
}
catch(Exception ex) {
Msg(ex.Message.ToString());
return false; }
finally { reg_ClassRoot.Close(); }
}
public void Msg(string message)
{
string rootPath = targetdirPath;
if (string.IsNullOrEmpty(rootPath)) {
rootPath = "D:\\runsoft\\meswfTest";
}
try
{
var _path = Path.Combine(rootPath, "Log");
if (!Directory.Exists(_path))
{
Directory.CreateDirectory(_path);
}
var _filepath = Path.Combine(_path, "Msg.LOG");
if (File.Exists(_filepath))
{
var _file = new FileInfo(_filepath);
if (_file.Length > 102400)
{
_file.MoveTo(Path.Combine(_path,
"Msg." +
DateTime.Now.ToString("yyyyMMddHHmmssfffffff") +
".LOG"));
}
}
using (FileStream fileStream = File.Open(_filepath, FileMode.Append, FileAccess.Write))
{
using (StreamWriter writer = new StreamWriter(fileStream))
{
writer.WriteLine(DateTime.Now.ToString());
writer.WriteLine(message);
writer.Close();
}
fileStream.Close();
}
}
catch (Exception _exception)
{
throw;
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
最后编译生成下。
2、将步骤1中的类库添加到安装项目setup中的项目输出中,
3、自定义操作
在setup项目上右键选择“视图”-》“自定义操作”,在安装和卸载操作上添加针对步骤2中的输出项的自定义操作。
![这里写图片描述](https://img-blog.csdn.net/20170209173528343?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc2hpc3Vpemhl/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
![这里写图片描述](https://img-blog.csdn.net/20170209173617687?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc2hpc3Vpemhl/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
在卸载操作上的操作同上
选择自定义操作下的“安装”-》“主输出来自ExtensionLibrary(活动)”,在其属相中CustomActionData中设置/targetdir=”[TARGETDIR]\”,此处是为了在步骤1的类库中能获取到当前安装路径。
![这里写图片描述](https://img-blog.csdn.net/20170209173950236?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc2hpc3Vpemhl/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
this.Context.Parameters["targetdir"].TrimEnd('\\');
完成以上步骤后,编译生成setup项目得到setup.exe安装包
4、安装setup.exe应用程序
具体安装步骤这里不多讲了,值得提一下的是第3步打包时,程序的默认路径设置在D盘的方式
5、网页中的调用
安装完winform应用程序后,在网页上面使用如下代码
<html>
<head></head>
<body>
<a href="tpswftest://62350">启动</a>
</body>
</html>
即可链接启动它
其中tpswftest是自定义的写入注册表的节点名称,62350是传入参数,可在.exe中获取
获取方式如下:
namespace mes_wf_test
{
static class Program
{
[STAThread]
static void Main(string[] args)
{
string key = "";
if (args != null && args.Count() > 0)
{
key = Regex.Match(args[0], @"(?<=://).+?(?=:|/|\Z)").Value;
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1(key));
}
}
}