msi安装部署

最近做了一个小项目,需求是这样的:由于公司的安装部署的项目较多,一旦升级,比较麻烦,需要客户自己打开已经下载好的msi包,自主安装,一旦客户没有安装到新版本,客户还会要求解决在新版本中已经解决的问题。针对这种情况,公司要求自动升级,在升级过程中不出现安装界面,客户每次打开的软件都要是最新的。

由于是第一次做安装部署方面的工作,所以一切从零开始,慢慢查资料,摸索了几天终于做成了,现总结此文。

由于是升级,所以客户机器里已经装了老版本的软件。升级一定要覆盖老版本,所以先要知道老版本的安装路径,这样保证老版本里的软件配置升级后不会改变。老的安装包没有设置注册表中InstallLocation,所以必须从注册表中找到老的安装路径,在注册表节点hkey_Local_Machine\Software\Microsoft\Windows\CurrentVersion\Installer\UserData下找。怎么找呢?首先需要知道msi包中其中一个文件的名称,这里找的是.exe文件,在上面的那个节点中找exe文件安装的位置。这里有问题出现了,怎么在msi包中找到exe文件的名称呢?经过一番查询,终于找到了方法,代码如下:

 

private string GetExeFileNamesFromMSI(string sourceLocation)
        {
            string exeName = string.Empty;

            System.Type oType = System.Type.GetTypeFromProgID("WindowsInstaller.Installer");
            Installer inst = System.Activator.CreateInstance(oType) as Installer;
            Database DB = inst.OpenDatabase(sourceLocation, MsiOpenDatabaseMode.msiOpenDatabaseModeReadOnly);
            string str = "SELECT * FROM File";

            WindowsInstaller.View thisView = DB.OpenView(str);
            thisView.Execute(null);
            WindowsInstaller.Record thisRecord = thisView.Fetch();
            string name = thisRecord.get_StringData(3);
            while (thisRecord != null)
            {
                if (!string.IsNullOrEmpty(name) && name.ToLower().EndsWith(".exe"))
                {
                    exeName = name.Substring(name.LastIndexOf('|') + 1);
                    break;
                }
                thisRecord = thisView.Fetch();
                if (thisRecord != null)
                {
                    name = thisRecord.get_StringData(3);
                }
            }
            return exeName;
        }

这里的sourceLocation就是msi包的路径。这里需要说明的是,为了使用WindowsInstaller.Installer,不要在工程中添加system32下的msi.dll。知道了exe文件的名称就可以在注册表节点下找路径了,这里的用递归的方法找,速度有点慢,方法如下:

        private string AccessRegistry(RegistryKey key)
        {
            string path = string.Empty;
            if (key.SubKeyCount > 0)
            {
                string[] subkeyNames = key.GetSubKeyNames();
                foreach (string subKeyName in subkeyNames)
                {
                    RegistryKey subKey = key.OpenSubKey(subKeyName);
                    if (path.EndsWith(this._productExeName))
                    {
                        return path;
                    }
                    path = AccessRegistry(subKey);
                }
            }
            else
            {
                if (key.ValueCount > 0)
                {
                    string[] subvalueNames = key.GetValueNames();
                    foreach (string subValueName in subvalueNames)
                    {
                        string location = key.GetValue(subValueName).ToString();
                        if (location.ToLower().Contains(this._productExeName.ToLower()))
                        {
                            if (location.EndsWith(this._productExeName))
                            {
                                return location;
                            }
                        }
                    }
                }
            }
            return path;
        }

这里的参数key就是

RegistryKey pregkey = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Installer\UserData");

把找到的路径中的最后的exe文件名称去掉就是安装软件的路径。找到了路径就可以覆盖安装了。这里我遇到了一个问题。我在installer项目中override OnCommitted方法,在方法中自动添加了注册表中InstallLocation这一项(具体添加方法稍后再说),但是覆盖安装的时候就是没写上,纳闷中,希望高手指点。所以采取了比较笨的方法,先卸掉老的,再安装新的,InstallLocation就被填上了。这里的安装采取的是系统的工具msiexec.exe,关于这个工具的用法,网上很多,不做介绍了。通过cmd执行指令,方法如下:

            try
            {
                Process proc = new Process();
                proc.StartInfo.FileName = "cmd.exe";

                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.RedirectStandardError = true;
                proc.StartInfo.RedirectStandardInput = true;
                proc.StartInfo.RedirectStandardOutput = true;

                proc.StartInfo.CreateNoWindow = true;
                proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

                proc.Start();
                if (!string.IsNullOrEmpty(uninstallString))
                {
                    uninstallString = uninstallString.Replace("/I", "/X");
                    proc.StandardInput.WriteLine(uninstallString + " /qn");
                }

                if (!string.IsNullOrEmpty(installLocation))
                {
                    proc.StandardInput.WriteLine("msiexec /i \"" + sourceLocation + "\" /qn TARGETDIR=\"" + installLocation + "\"");
                }
                else
                {
                    proc.StandardInput.WriteLine("msiexec /i \"" + sourceLocation + "\" /qn");
                }
                proc.StandardInput.WriteLine("exit");

                output = proc.StandardOutput.ReadToEnd();
                proc.WaitForExit();
                proc.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }

这里需要说明的是qn是静默安装,还有其他参数,自己找找就知道了。uninstallString是从注册表中获取的,下面代码中,由于这个自动升级工程是公共的,并且是重复使用的,所以必须考虑第二次自动升级的时候老的安装路径是已经记录下了,不用再去上面的那个注册表节点中查询了,直接根据产品名称在注册表中找到老版本软件的卸载信息。这里的产品名称也可以从msi包中找到。方法如下:

private string GetProductNameFromMSI(string sourceLocation)
        {
            System.Type oType = System.Type.GetTypeFromProgID("WindowsInstaller.Installer");
            Installer inst = System.Activator.CreateInstance(oType) as Installer;
            Database DB = inst.OpenDatabase(sourceLocation, MsiOpenDatabaseMode.msiOpenDatabaseModeReadOnly);
            string str = "SELECT * FROM Property WHERE Property = 'ProductName'";

            WindowsInstaller.View thisView = DB.OpenView(str);
            thisView.Execute(null);
            WindowsInstaller.Record thisRecord = thisView.Fetch();
            string name = thisRecord.get_StringData(2);
            return name;
        }

当然如果SELECT * FROM Property也可以多次Fetch()得到其他的信息。根据产品名称即卸载信息里的DisplayName,找到注册表中软件的卸载信息,方法如下:

            RegistryKey currentKey = null;
            using (RegistryKey pregkey = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Uninstall"))
            {
                //Get all keys
                foreach (string item in pregkey.GetSubKeyNames())
                {
                    currentKey = pregkey.OpenSubKey(item);
                    if (currentKey.GetValue("DisplayName") != null)
                    {
                        displayName = currentKey.GetValue("DisplayName").ToString();           //Get display name


                        if (displayName == productName)
                        {
                            string oldInstallLocation = currentKey.GetValue("InstallLocation").ToString();
                            uninstallString = currentKey.GetValue("UninstallString").ToString();

                            if (!string.IsNullOrEmpty(oldInstallLocation))
                            {
                                Regex reg = new Regex(@"^(?<fpath>([a-zA-Z]:\\)([\s\.\-\w]+\\)*)(?<fname>[\w]+.[\w]+)");
                                Match result = reg.Match(oldInstallLocation);
                                if (result.Success)
                                {
                                    installLocation = oldInstallLocation;
                                }
                            }
                            break;
                        }
                    }
                }

这里的正则表达式是判断路径的。这里的uninstallString就是上面卸载的时候用到的指令。oldInstallLocation就是注册表中老版本中的InstallLocation项记录的内容,也是控制面板中显示的内容。

到这里自动升级的工作就做完了。

之前有提到Installer的配置问题,这里也顺便说一下,至于Installer的添加,应用什么的网上很多,msdn说的也很清除,可以自己翻翻看。我这里说的就是如何获取msi安装界面上的参数,在静默安装中如何传递参数,如何在Installer中自己控制安装动作起其他。

首先建立了Installer的工程后,override OnCommitted方法,如下:

        protected override void OnCommitted(System.Collections.IDictionary savedState)
        {
            base.OnCommitted(savedState);
            try
            {
                if (savedState.Contains("ProductID"))
                {
                    string productId = savedState["ProductID"].ToString();
                    RegistryKey applicationRegistry = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" + productId, true);

                    if (applicationRegistry != null)
                    {
                        if (savedState.Contains("TargetDir"))
                        {
                            applicationRegistry.SetValue("InstallLocation", savedState["TargetDir"].ToString());
                        }
                    }
                    applicationRegistry.Close();
                }
            }
            catch
            { }
        }

由于我们公司发软件是根据ProductCode在注册表中标记的,所以知道ProductCode后一下子就可以定位到软件的卸载信息,这里是把安装路径写到InstallLocation的注册表项中。

那么如何把参数从setup工程中传递到Installer类中呢?这里需要把Installer工程引用到setup工程中,具体如何引用网上有很多,不做说明了。这里需要说明的是,setup工程中Custom Action视图中有Install,Commit,Rollback,Uninstall四个,你在那一个添加了Primary output from XX,XX就是Installer类所在的工程名称,就会相应触发Installer类中的方法,比如你在Commit中添加了Action,那么Installer类OnCommitted等就会触发(错了别拍砖,没有查证呢)。那么如何传递参数呢?

在Custom Action视图的Install节点下添加Primary output from XX,在属性项CostomActiionData中加入2个参数分别为:/DP_TargetDir="[TARGETDIR]\" /DP_ProductID="[ProductCode]",别往了以空格分开,并在Installer类中的public override void Install(System.Collections.IDictionary stateSaver)方法中加入如下代码:

            if (Context.Parameters.ContainsKey("DP_TargetDir") &&
                Context.Parameters.ContainsKey("DP_ProductID"))
            {
                stateSaver.Add("TargetDir", Context.Parameters["DP_TargetDir"].ToString());
                stateSaver.Add("ProductID", Context.Parameters["DP_ProductID"].ToString());
            }

由于System.Collections.IDictionary stateSaver这个字典是全局的,所以在Install(System.Collections.IDictionary stateSaver)方法中加入后,在OnCommitted(System.Collections.IDictionary savedState)中就能读取到。

 

全文完。








 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值