Search code examples
visual-studiovisual-studio-2008setup-project

How to include version number in VS Setup Project output filename


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

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.


Solution

  • 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.

    Here is what you can do:

    In the post build event ->

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

    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.

    Getting msi properties code is used from alteridem article

    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;
        }
      }