• vs install 安装时自动添加注册表


    思路:使用自定义

    解决方案添加类库项目

    添加安装程序类

    随后右键查看代码

    在构造函数添加事件

    同时完成这个事件,在此事件中根据需要添加我们需要的内容,此处为添加注册表,并根据安装目录添加url protocol协议

    代码如下

    public Installer()
            {
                InitializeComponent();
                this.AfterInstall += new InstallEventHandler(Installer_AfterInstall);
            }
    
            private void Installer_AfterInstall(object sender, InstallEventArgs e)
            {
                string path = this.Context.Parameters["targetdir"];//获取用户设定的安装目标路径, 注意,需要在Setup项目里面自定义操作的属性栏里面的CustomActionData添加上/targetdir="[TARGETDIR]"
                List<string> cmds =
                new List<string>{
                   "/c" + $"reg add "HKCR\EasyPrint" /f /ve  /d "EasyPrintProtocol"",
                   "/c" + $"reg add "HKCR\EasyPrint" /f /v "URL Protocol"  /d "",
                   "/c" + $"reg add "HKCR\EasyPrint\DefaultIcon" /f /ve  /d ""+path+"\EasyPrint.exe,1"",
                   "/c" + $"reg add "HKCR\EasyPrint\shell\open\command" /f /ve  /d "\""+path+"\EasyPrint.exe\"  \"%1\"""
                };
                foreach (var command in cmds)
                {
                    Process p = new Process
                    {
                        StartInfo =
                        {
                            FileName = "cmd.exe",
                            Arguments = command,
                            UseShellExecute = false,
                            RedirectStandardInput = true,
                            RedirectStandardOutput = true,
                            CreateNoWindow = true
                        }
                    };
                    p.Start();
                    p.StandardInput.WriteLine("exit");
                    p.Close();
                }
            }

     在install项目中添加此类库的主输出

    右键项目,view,自定义操作

     

    在需要的位置添加自定义操作

    右键

    右键项目

     

     这里选择我们需要的项目,即自动添加注册表的主输出

     

    右键属性 在CustomActionData中加入/targetdir="[TARGETDIR]/"

    这样便可在代码中使用this.Context.Parameters["targetdir"]获取安装路径

    编译即可

  • 相关阅读:
    解决IE8不兼容通过class名获取元素的方法
    移动端页面遇到过的各种坑
    强大的正则表达式
    弹性盒子布局
    vue环境搭建
    fullpage.js使用指南
    ES5原生api(1)
    双色球中奖率分析(python)
    使用python脚本的3D引擎Panda3d
    Python lambda介绍
  • 原文地址:https://www.cnblogs.com/ives/p/10918650.html
Copyright © 2020-2023  润新知