如何在 VS 安装项目输出文件名中包含版本号 [英] How to include version number in VS Setup Project output filename

查看:16
本文介绍了如何在 VS 安装项目输出文件名中包含版本号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 VS2008 安装项目中包含版本号作为 output.msi 文件名的一部分?

Is there a way to include the version number as part of the output.msi filename in a VS2008 Setup Project?

例如,我想要一个名为myinstaller-1.0.13.msi"的输出文件,其中版本部分是根据我在部署项目属性中放入的版本号自动设置的.

I'd like for example an output file called: "myinstaller-1.0.13.msi" where the version part is automatically set based on the version number I have put in the deployment project properties.

推荐答案

不确定您是否仍然需要这个,但想要回答这个问题,因为我们在 postbuild 事件中做了类似的操作.就我所做的研究而言,不可能通过设置过程在内部设置您想要的文件名.

Not sure whether you still require this or not but wanted answer this as we did similar kind of operation in the postbuild event. As far as the research I did this is not possible to set the file name as you want internally through setup process.

您可以通过在构建后事件中通过外部应用程序命名输出文件来以其他方式执行此操作.

You can do this in other way by naming the output file through an external application in post build event.

您可以这样做:

在后期构建事件中 ->

In the post build event ->

[MsiRenamerAppPath]MsiRenamer.exe "$(BuildOutputPath)"

[MsiRenamerAppPath]MsiRenamer.exe "$(BuildOutputPath)"

创建一个应用程序,该应用程序将使用部署项目的版本号重命名 msi 文件.以下是用于该应用程序的代码.我想这应该可以满足您的要求.

Create an application which will rename the msi file with the version number from the deployment project. Following is the code used for the application. This should fulfill your requirement I guess.

替代文章

class MsiRenamer
  {
    static void Main(string[] args)
    {
      string inputFile;
      string productName = "[ProductName]";

      if (args.Length == 0)
      {
        Console.WriteLine("Enter MSI file:");
        inputFile = Console.ReadLine();
      }
      else
      {
        inputFile = args[0];
      }

      try
      {
        string version;

        if (inputFile.EndsWith(".msi", StringComparison.OrdinalIgnoreCase))
        {
          // Read the MSI property
          version = GetMsiProperty(inputFile, "ProductVersion");
          productName = GetMsiProperty(inputFile, "ProductName");
        }
        else
        {
          return;
        }
        // Edit: MarkLakata: .msi extension is added back to filename
        File.Copy(inputFile, string.Format("{0} {1}.msi", productName, version));
        File.Delete(inputFile);
      }
      catch (Exception ex)
      {
        Console.WriteLine(ex.Message);
      }
    }

    static string GetMsiProperty(string msiFile, string property)
    {
      string retVal = string.Empty;

      // Create an Installer instance  
      Type classType = Type.GetTypeFromProgID("WindowsInstaller.Installer");
      Object installerObj = Activator.CreateInstance(classType);
      Installer installer = installerObj as Installer;

      // Open the msi file for reading  
      // 0 - Read, 1 - Read/Write  
      Database database = installer.OpenDatabase(msiFile, 0);

      // Fetch the requested property  
      string sql = String.Format(
          "SELECT Value FROM Property WHERE Property='{0}'", property);
      View view = database.OpenView(sql);
      view.Execute(null);

      // Read in the fetched record  
      Record record = view.Fetch();
      if (record != null)
      {
        retVal = record.get_StringData(1);
        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(record);
      }
      view.Close();
      System.Runtime.InteropServices.Marshal.FinalReleaseComObject(view);
      System.Runtime.InteropServices.Marshal.FinalReleaseComObject(database);

      return retVal;
    }
  }

这篇关于如何在 VS 安装项目输出文件名中包含版本号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆