因为持续集成需要,所有项目编译完之后生成一个多语言的安装包。之前生成mst文件都是手动操作,而且mst文件必须每次重新和新的安装包“关联”,否则中文的安装包去调用英文的资源的时候就会报类似于“类型转换失败”的错误。基于这两点,有必要程序化这个流程,不然每次打包都得找我了。以下是程序的记录。比较简单。

     其实就是用程序调用cmd,再次之前,请记得将wix的bin目录加入到系统变量中。否则命令不会被识别;然后将程序执行目录指向目标目录。 

 static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                return;
            }
            Console.WriteLine(args[0]);

            var path = args[0];
          //  var path =MsiPath;
            
            Console.WriteLine("正在执行:" + DateTime.Now);
            var p = new Process
            {
                StartInfo =
                {
                    FileName = "cmd.exe ",
                    UseShellExecute = false,
                    RedirectStandardInput = true,
                    RedirectStandardOutput = true,
                    RedirectStandardError = true,
                    CreateNoWindow = false,
                    WorkingDirectory = path
                }
            };
            try
            {
                p.Start();

                if (!Init(p, path))
                {
                    Console.WriteLine("初始化失败");
                    p.Close();
                    return;
                }
                //合并语言
                ExcComand(p, CommandLines.LanZh_TW);
                ExcComand(p, CommandLines.LanZh_CN);
                ExcComand(p, CommandLines.LanEN_US);
                
                //关联语言
                ExcComand(p, CommandLines.CombanLanZh_cn);
                //测试安装包
               // ExcComand(p, CommandLines.Testzh_Tw);

                //p.WaitForExit();
                p.Close();
                Console.WriteLine("Complete" + DateTime.Now);
            }
            catch (Exception e1)
            {
                Console.WriteLine("error" + e1.Message);
            }

        }

  先初始化,确保安装文件、vbs文件和mst文件都存在。

  private static string[] lans = { "zh-cn", "en-us", "zh-tw" };
        private static bool Init(Process p, string path)
        {
            //多语言安装包是否存在 
            foreach (var lan in lans)
            {
                var lanPath = Path.Combine(path, lan, SoftName);
                if (!File.Exists(lanPath))
                {
                    Console.WriteLine("{0}安装包不存在!",lan);
                    return false;
                }
            }
            //保证两个文件
            CopyFileIfNotExist(path,"WixSubStg.vbs");
            CopyFileIfNotExist(path, "WiLangId.vbs");

            //变形文件是否存在
            var twmst = Path.Combine(path, "transforms", "zh-tw.mst");
            if (!File.Exists(twmst))
            {
                GetTransforms(p);
            }

            //再检测一次
            return File.Exists(twmst);
        }

这两个vbs文件,在教程十三里面有提供。 最后在检查一次mst文件就是确保mst文件的存在。变形文件是我们多语言安装包的关键。

  /// <summary>
        /// 生成变形文件
        /// </summary>
        /// <param name="p"></param>
        private static void GetTransforms(Process p)
        {
            ExcComand(p, CommandLines.EnToZh);
            ExcComand(p, CommandLines.EnToTw);
            ExcComand(p, CommandLines.ZhToEn);
        }

        private static void ExcComand(Process p, string command)
        {
            p.StandardInput.WriteLine(command);
            Console.WriteLine("command:" + command);
            Thread.Sleep(1500);
        }
View Code

相关文章: