Generation of WIX plug-in installation package (2) Automatic software update

The previous article recorded how to generate the simplest application installation package. Generally speaking, it is relatively enough for simple applications, but if you want to achieve automatic updates, you need to spend more effort. This article introduces a local area network-based company's internal plug-in update that I use. The implementation method is relatively simple. It is mainly divided into two parts: wix file configuration and cs file modification.
First, the wix environment configuration

<Upgrade Id="7a545a88-caad-43d6-ae74-4f5f9628f7e6">
      <UpgradeVersion OnlyDetect="no" Property="PREVIOUSFOUND"
          Minimum="0.0.0.0" IncludeMinimum="yes" 
          Maximum="!(bind.assemblyVersion.tempAssembly)" IncludeMaximum="no" />
</Upgrade>

Insert such a piece of content (Upgrade) in the Product section. It should be noted that the Id of the Upgrade is the same as the UpgradeCode of the Product, and the maximum version number Maximum. Binding version information: bind.assemblyVersion.tempAssembly: The assemblyVersion item represents the version file "assemblyInfo.cs" of

[assembly: AssemblyVersion("1.0.0.0")]
将其改为[assembly: AssemblyVersion("1.0.*")]

tempAssembly represents the Id of the File tag of the file to which version information is to be bound, and its Assembly value is set to ".net", that is:

<Component Id="ProductComponent">
        <File Id="tempAssembly" Source="$(var.temp.TargetPath)"  KeyPath="yes" Assembly=".net"/>
</Component>

After that, modify the Version of Product to "!(bind.assemblyVersion.tempAssembly)", so we have completed the configuration of the Windows Installer XML file.
2. CS code modification
This part is mainly divided into two parts, one is to modify the project version file, which has been recorded just now; the other is to identify and update the application version (compared with the version of the installation package in the local area network).

//Program中新建一个Update方法
static bool Update()
        {
            bool ret = false;
            try
            {
                //安装包路径
                string msifile = @"C:\MyDoc\C# practice\test\testUpdate\bin\Debug\testUpdate.msi";
                //这部分主要是获取安装包版本信息,基本通用,注意导入WindowsInstaller才能使用
                System.Type oType = System.Type.GetTypeFromProgID("WindowsInstaller.Installer");
                Installer installer = System.Activator.CreateInstance(oType) as Installer;
                Database DB = installer.OpenDatabase(msifile, MsiOpenDatabaseMode.msiOpenDatabaseModeReadOnly);
                WindowsInstaller.View view = DB.OpenView("SELECT * FROM Property WHERE Property = 'ProductVersion'");
                view.Execute();
                WindowsInstaller.Record rec = view.Fetch();
                string result = rec.get_StringData(2);//安装包版本
                //现在运行程序的版本
                FileVersionInfo oversion = FileVersionInfo.GetVersionInfo(System.Windows.Forms.Application.ExecutablePath);
                if (oversion.FileVersion.CompareTo(result) < 0)
                {
                    MessageBox.Show("已有新版本:" + result + ",确定更新!");
                    Process.Start(msifile);
                    ret = true;
                }
            }
            catch (Exception)
            {
                MessageBox.Show("未找到版本信息");
            }
            return ret;
        }

This is written in the main program

static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            if (!Update())
            {
                Application.Run(new Form1());//你自己的程序
            }
        }

3. The last step
Since we used Assembly=”.net”, we need to add a strong name to the project with version information to install the msi installation package.
Select the project in the solution manager, right-click and select "Properties"
write picture description here
to create a new strong name key file at the signature. At this point, regenerate all the projects, and actively generate the wix project to get the installation package we want. After recompiling and generating the installation package, we can automatically prompt the update when we open the software.

Fourth, the problem that the installation files are not covered The frequency of generating installation packages during the
debugging phase is very high, and the automatically generated version numbers are generally generated in units of time, so if the time interval is too close, the version numbers are too close, so that the source files will not be overwritten ( In general, if it is ABCD, at least C changes will cover the source file), so don't mind this problem if there will be no frequent changes in the future.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325874186&siteId=291194637