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

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

2023年7月3日发(作者:)

程序打包修改注册表使其可通过⽹页触发启动并传⼊参数winform程序的打包winform的打包这⾥不展开,百度下很多。

1、⾸页创建⼀个类库项⽬,添加⼀个安装程序类,往⾥添加两个事件,然后往两个事件中编写修改注册表的代码//安装完成后nstall += new InstallEventHandler(InstallerDemo_AfterInstall);//卸载完成后ninstall += new InstallEventHandler(InstallerDemo_AfterUninstall);如下图所⽰:

整体代码如下:[RunInstaller(true)] public partial class InstallerDemo : ler { public InstallerDemo() { InitializeComponent(); nstall += new InstallEventHandler(InstallerDemo_AfterInstall); ninstall += new InstallEventHandler(InstallerDemo_AfterUninstall); } private string targetdirPath = ""; ///

/// 安装完成后触发 /// /// /// void InstallerDemo_AfterInstall(object sender, InstallEventArgs e) { //throw new NotImplementedException(); Msg("注册表信息添加开始:"); //Msg("安装路径:" + targetdirPath); Msg("⽂件路径:" + e(targetdirPath, "mes_wf_")); var result= RegeditAdd("tpswftest", e(targetdirPath, "mes_wf_"), ""); Msg("注册表修改是否成功:" + result); } /// /// 卸载完成之后触发 /// targetdirPath = ters["targetdir"].TrimEnd('');//获取⽤户设定的安装⽬标路径, 注意,需要在Setup项⽬⾥⾯⾃定义操作的属性栏⾥⾯ /// /// /// void InstallerDemo_AfterUninstall(object sender, InstallEventArgs e) { //throw new NotImplementedException(); Msg("注册表信息删除开始:"); var result= RegeditDelete("tpswftest"); Msg("注册表信息删除:"+result); } /// /// 注册协议 /// /// 根节点 /// 应⽤程序路径 /// 应⽤程序打开图标,可选值 /// public bool RegeditAdd(string Root_Key, string file_application_path, string file_application_ico) { //获取注册表HKEY_CLASSES_ROOT RegistryKey reg_ClassRoot = sRoot; try { RegistryKey reg_key = reg_bKey(Root_Key, true); if (reg_key == null) { //创建⼦节点HKEY_CLASSES_ROOTtpswftest RegistryKey reg_sjbs = reg_SubKey(Root_Key); //添加默认项 reg_ue("", "URL: " + Root_Key + " Protocol Handler"); //协议别名 reg_ue("URL Protocol", file_application_path); //创建[HKEY_CLASSES_ROOTtpswftestDefaultIcon] RegistryKey reg_DefaultIcon = reg_SubKey("DefaultIcon"); if (!OrEmpty(file_application_ico)) { //设置⾃定义图标 reg_ue("", file_application_ico); } else { //设置系统定义图标 reg_ue("", file_application_path + ",1"); } //创建呼出处理程序[HKEY_CLASSES_ROOTtpswftestshellopencommand] RegistryKey reg_command = reg_SubKey("shell").CreateSubKey("open").CreateSubKey("command"); //%1 表⽰传递的参数,再次%1表⽰调⽤处显⽰链接⽂本 reg_ue("", """ + file_application_path + "" "%1""); } return true; } catch(Exception ex) { Msg(ng()); return false; } finally { reg_(); } } /// /// 删除协议 /// /// 根节点 /// public bool RegeditDelete(string Root_Key) { //获取注册表HKEY_CLASSES_ROOT RegistryKey reg_ClassRoot = sRoot; try { //获取注册表[HKEY_CLASSES_ROOTtpswftest]项 RegistryKey reg_sjbs = reg_bKey(Root_Key, true); if (reg_sjbs != null) { reg_SubKeyTree(Root_Key); return true; } else { Msg("注册表中" + reg_ClassRoot + "" + Root_Key + "不存在!"); return false; } } catch(Exception ex) { //添加错误⽇志 Msg(ng()); return false; } finally { reg_(); } } /// /// ⽇志 /// /// public void Msg(string message) { string rootPath = targetdirPath; if (OrEmpty(rootPath)) { rootPath = "D:runsoftmeswfTest"; } try { var _path = e(rootPath, "Log"); if (!(_path)) { Directory(_path); } var _filepath = e(_path, ""); if ((_filepath)) { var _file = new FileInfo(_filepath); if (_ > 102400) { _(e(_path, "Msg." + ng("yyyyMMddHHmmssfffffff") + ".LOG")); } } using (FileStream fileStream = (_filepath, , )) { using (StreamWriter writer = new StreamWriter(fileStream)) { ine(ng()); ine(message); (); } (); } } catch (Exception _exception) { throw; } } }最后编译⽣成下。

2、将步骤1中的类库添加到安装项⽬setup中的项⽬输出中,

3、⾃定义操作

在setup项⽬上右键选择“视图”-》“⾃定义操作”,在安装和卸载操作上添加针对步骤2中的输出项的⾃定义操作。

在卸载操作上的操作同上

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

ters["targetdir"].TrimEnd('');完成以上步骤后,编译⽣成setup项⽬得到安装包

4、安装应⽤程序

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

5、⽹页中的调⽤

安装完winform应⽤程序后,在⽹页上⾯使⽤如下代码启动即可链接启动它

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

获取⽅式如下:namespace mes_wf_test{ static class Program { ///

/// 应⽤程序的主⼊⼝点。 /// [STAThread] //static void Main() //{ // VisualStyles(); // patibleTextRenderingDefault(false); // (new Form1()); //} static void Main(string[] args) { string key = ""; if (args != null && () > 0) { key = (args[0], @"(?<=://).+?(?=:|/|Z)").Value; } VisualStyles(); patibleTextRenderingDefault(false); (new Form1(key)); } }}

发布者:admin,转转请注明出处:http://www.yc00.com/xiaochengxu/1688383606a129855.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信