winform .exe程序打包修改注册表使其可通过网页触发启动并传入参数
2017-02-09 17:21
811 查看
本篇主要实现的是在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
下面我做了自己的整理:
1、首页创建一个类库项目,添加一个安装程序类,往里添加两个事件,然后往两个事件中编写修改注册表的代码
如下图所示:
整体代码如下:
最后编译生成下。
2、将步骤1中的类库添加到安装项目setup中的项目输出中,
3、自定义操作
在setup项目上右键选择“视图”-》“自定义操作”,在安装和卸载操作上添加针对步骤2中的输出项的自定义操作。
在卸载操作上的操作同上
选择自定义操作下的“安装”-》“主输出来自ExtensionLibrary(活动)”,在其属相中CustomActionData中设置/targetdir=”[TARGETDIR]\”,此处是为了在步骤1的类库中能获取到当前安装路径。
完成以上步骤后,编译生成setup项目得到setup.exe安装包
4、安装setup.exe应用程序
具体安装步骤这里不多讲了,值得提一下的是第3步打包时,程序的默认路径设置在D盘的方式
5、网页中的调用
安装完winform应用程序后,在网页上面使用如下代码
即可链接启动它
其中tpswftest是自定义的写入注册表的节点名称,62350是传入参数,可在.exe中获取
获取方式如下:
网上也找了很多资料,主要结合了以下两位的技术分享
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);
如下图所示:
整体代码如下:
[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; } } }
最后编译生成下。
2、将步骤1中的类库添加到安装项目setup中的项目输出中,
3、自定义操作
在setup项目上右键选择“视图”-》“自定义操作”,在安装和卸载操作上添加针对步骤2中的输出项的自定义操作。
在卸载操作上的操作同上
选择自定义操作下的“安装”-》“主输出来自ExtensionLibrary(活动)”,在其属相中CustomActionData中设置/targetdir=”[TARGETDIR]\”,此处是为了在步骤1的类库中能获取到当前安装路径。
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 { /// <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)); } } }
相关文章推荐
- winform .exe程序打包修改注册表使其可通过网页触发启动并传入参数
- 通过注册表让烦人程序不再自动启动
- Eclipse java项目打包工具(fatjar)、Java EXE 启动文件生成程序
- WINFORM程序打包EXE文件
- Windows2000/xp通过修改注册表禁用程序的方法
- 在函数中不修改函数传入参数的情况下取到调用程序中的值的方法
- 通过注册表让烦人程序不再自动启动
- C# Winform打包部署时添加注册表信息实现开机启动
- 通过注册表修改WINCE系统IP参数
- 通过网页修改activex安全设置,添加信任站点,禁用弹出窗口阻止程序
- 用WIN PE通过修改系统注册表修复因盘符错乱而导致系统无法启动问题
- 通过修改注册表让wince开机自动运行程序,不需要修改内核
- 修改注册表开机自动启动程序
- 通过修改启动参数修改Netbeans 6.x 的语言
- vs2005打包程序 出现“验证时出错 ”修改注册表
- 通过网页修改activex安全设置,添加信任站点,禁用弹出窗口阻止程序
- Eclipse java项目打包工具(fatjar)、Java EXE 启动文件生成程序
- 通过程序修改注册表键值来达到修改IE配置参数的目的
- 通过网页修改activex安全设置,添加信任站点,禁用弹出窗口阻止程序
- winform带参数启动另一个exe