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"]获取安装路径

 

 

编译即可

posted @ 2019-05-24 16:02  Hey,Coder!  阅读(811)  评论(0编辑  收藏  举报