您的位置:首页 > 其它

winform .exe程序打包修改注册表使其可通过网页触发启动并传入参数

2017-11-22 19:05 621 查看
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程序的打包

winform的打包这里不展开,百度下很多。 

1、首页创建一个类库项目,添加一个安装程序类,往里添加两个事件,然后往两个事件中编写修改注册表的代码
//安装完成后
this.AfterInstall += new InstallEventHandler(InstallerDemo_AfterInstall);
//卸载完成后
this.AfterUninstall += new InstallEventHandler(InstallerDemo_AfterUninstall);
1
2
3
4

如下图所示: 



整体代码如下:
[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 = "";
/// <summary>
/// 安装完成后触发
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void InstallerDemo_AfterInstall(object sender, InstallEventArgs e)
{
//throw new NotImplementedException();
targetdirPath = this.Context.Parameters["targetdir"].TrimEnd('\\');//获取用户设定的安装目标路径, 注意,需要在Setup项目里面自定义操作的属性栏里面的CustomActionData添加上/targetdir="[TARGETDIR]\"
Msg("注册表信息添加开始:");
//Msg("安装路径:" + targetdirPath);
Msg("文件路径:" + Path.Combine(targetdirPath, "mes_wf_test.exe"));
var result= RegeditAdd("tpswftest", Path.Combine(targetdirPath, "mes_wf_test.exe"), "");
Msg("注册表修改是否成功:" + result);
}
/// <summary>
/// 卸载完成之后触发
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void InstallerDemo_AfterUninstall(object sender, InstallEventArgs e)
{
//throw new NotImplementedException();
Msg("注册表信息删除开始:");
var result= RegeditDelete("tpswftest");
Msg("注册表信息删除:"+result);
}
/// <summary>
/// 注册协议
/// </summary>
/// <param name="Root_Key">根节点</param>
/// <param name="file_application_path">应用程序路径</param>
/// <param name="file_application_ico">应用程序打开图标,可选值</param>
/// <returns></returns>
public bool  RegeditAdd(string Root_Key, string file_application_path, string file_application_ico)
{
//获取注册表HKEY_CLASSES_ROOT
RegistryKey reg_ClassRoot = Registry.ClassesRoot;
try
{
RegistryKey reg_key = reg_ClassRoot.OpenSubKey(Root_Key, true);
if (reg_key == null)
{
//创建子节点HKEY_CLASSES_ROOT\tpswftest
RegistryKey reg_sjbs = reg_ClassRoot.CreateSubKey(Root_Key);
//添加默认项
reg_sjbs.SetValue("", "URL: " + Root_Key + " Protocol Handler");
//协议别名
reg_sjbs.SetValue("URL Protocol", file_application_path);
//创建[HKEY_CLASSES_ROOT\tpswftest\DefaultIcon]
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");
}
//创建呼出处理程序[HKEY_CLASSES_ROOT\tpswftest\shell\open\command]
RegistryKey reg_command = reg_sjbs.CreateSubKey("shell").CreateSubKey("open").CreateSubKey("command");
//%1 表示传递的参数,再次%1表示调用处显示链接文本
reg_command.SetValue("", "\"" + file_application_path + "\" \"%1\"");
}
return true;
}
catch(Exception ex) {
Msg(ex.Message.ToString());
return false; }
finally { reg_ClassRoot.Close(); }
}

/// <summary>
/// 删除协议
/// </summary>
/// <param name="Root_Key">根节点</param>
/// <returns></returns>
public bool RegeditDelete(string Root_Key)
{
//获取注册表HKEY_CLASSES_ROOT
RegistryKey reg_ClassRoot = Registry.ClassesRoot;
try
{
//获取注册表[HKEY_CLASSES_ROOT\tpswftest]项
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(); }
}
/// <summary>
/// 日志
/// </summary>
/// <param name="message"></param>
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中的输出项的自定义操作。 


 





在卸载操作上的操作同上 

选择自定义操作下的“安装”-》“主输出来自ExtensionLibrary(活动)”,在其属相中CustomActionData中设置/targetdir=”[TARGETDIR]\”,此处是为了在步骤1的类库中能获取到当前安装路径。 



this.Context.Parameters["targetdir"].TrimEnd('\\');
1

完成以上步骤后,编译生成setup项目得到setup.exe安装包 


 

4、安装setup.exe应用程序 

具体安装步骤这里不多讲了,值得提一下的是第3步打包时,程序的默认路径设置在D盘的方式


 

5、网页中的调用 

安装完winform应用程序后,在网页上面使用如下代码
<html>
<head></head>
<body>
<a href="tpswftest://62350">启动</a>
</body>
</html>
1
2
3
4
5
6

即可链接启动它 


 

其中tpswftest是自定义的写入注册表的节点名称,62350是传入参数,可在.exe中获取 

获取方式如下:
namespace mes_wf_test
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
//static void Main()
//{
//    Application.EnableVisualStyles();
//    Application.SetCompatibleTextRenderingDefault(false);
//    Application.Run(new Form1());
//}
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));
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: