Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically change a project's product version?

I have several deployment projects. In order to deploy an application, I need to do several tasks, one of them is to change each deployment project's product version and product code.

I can't find a way to programmatically change them.

Since it's a Deployment project (which finally produces an executable installer), I'm not able to work with MSBuild, instead I'm using the Devenv from the command prompt.

like image 764
El Padrino Avatar asked Nov 20 '08 17:11

El Padrino


2 Answers

I was searching for the exact same thing today. I found this using google:

static void Main(string[] args) 
{
    string setupFileName = @"<Replace the path to vdproj file>"; 
    StreamReader reader = File.OpenText(setupFileName); 
    string file = string.Empty; 

    try 
    { 
        Regex expression = new Regex(@"(?:\""ProductCode\"" = 
        \""8.){([\d\w-]+)}"); 
        Regex expression1 = new Regex(@"(?:\""UpgradeCode\"" = 
        \""8.){([\d\w-]+)}"); 
        file = reader.ReadToEnd(); 

        file = expression.Replace(file, "\"ProductCode\" = \"8:{" + 
        Guid.NewGuid().ToString().ToUpper() + "}"); 
        file = expression1.Replace(file, "\"UpgradeCode\" = \"8:{" 
        + Guid.NewGuid().ToString().ToUpper() + "}"); 
    } 
    finally 
    { 
        // Close the file otherwise the compile may not work 
        reader.Close(); 
    } 

    TextWriter tw = new StreamWriter(setupFileName); 
    try 
    { 
        tw.Write(file); 
    } 
    finally 
    { 
        // close the stream 
        tw.Close(); 
    } 
 }
like image 102
TheCodeMonk Avatar answered Dec 24 '22 22:12

TheCodeMonk


I know that the original poster is looking for a .NET 2.0 solution to this problem. However, since this wasn't tagged as .NET, I'll offer up my C++ solution to the problem. This may be applicable in .NET land, but I'll leave that to others.

This not only updates the version information in the about box and log file for my application, but also all of the Windows version info that is seen in Windows Explorer.

UPDATE: Added some changes that I've made to the process since my original answer.

First off, I moved the entire version info block from my Project.rc file to my Project.rc2 file:

/////////////////////////////////////////////////////////////////////////////
//
// Version
//

VS_VERSION_INFO VERSIONINFO
 FILEVERSION FILE_VER
 PRODUCTVERSION PROD_VER
 FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
  FILEFLAGS 0x1L
#else
  FILEFLAGS 0x0L
#endif
  FILEOS 0x4L
  FILETYPE 0x1L
  FILESUBTYPE 0x0L
BEGIN
   BLOCK "StringFileInfo"
   BEGIN
       BLOCK "040904e4"
       BEGIN
           VALUE "CompanyName", "MyCompany"
           VALUE "FileDescription", "Software Description"
           VALUE "FileVersion", 1,0,0,1
           VALUE "InternalName", "FileName.exe"
           VALUE "LegalCopyright", "(c) 2008 My Company.  All rights reserved."
           VALUE "OriginalFilename", "FileName.exe"
           VALUE "ProductName", "Product Name"
           VALUE "ProductVersion", 1,0,0,1
       END
   END
   BLOCK "VarFileInfo"
   BEGIN
       VALUE "Translation", 0x409, 1252
   END
END

This essentially transports all of the version info stuff that you would edit from the resource editor into a separate file. This makes it so that you don't get errors when editing the resource file from outside of the editor. The downside is that you can no longer edit the version info from the resource editor. But, since we want this stuff updated automatically, that isn't a big deal.

Next, I created a VersionInfo.h file and added it to my project:

#pragma once

//major release version of the program, increment only when major changes are made
#define VER_MAJOR 2

//minor release version of the program, increment if any new features are added
#define VER_MINOR 0

//any bugfix updates, no new features
#define VER_REV 0

//if this is some special release (e.g. Alpha 1) put the special release string here
#define STR_SPECIAL_REL "Alpha 1"


#define FILE_VER VER_MAJOR,VER_MINOR,VER_REV
#define PROD_VER FILE_VER

//these are special macros that convert numerical version tokens into string tokens
//we can't use actual int and string types because they won't work in the RC files
#define STRINGIZE2(x) #x
#define STRINGIZE(x) STRINGIZE2(x)

#define STR_FILE_VER STRINGIZE(VER_MAJOR) "." STRINGIZE(VER_MINOR) "." STRINGIZE(VER_REV)
#define STR_PROD_VER STR_FILE_VER " " STR_SPECIAL_REL

#define STR_COPYRIGHT_INFO "©" BuildYear " Your Company. All rights reserved."

I then included VersionInfo.h in the rc2 file and made the following changes:

#include "VersionInfo.h"
/////////////////////////////////////////////////////////////////////////////
//
// Version
//

<no changes>
           VALUE "FileVersion", STR_FILE_VER
           <no changes>
           VALUE "LegalCopyright", STR_COPYRIGHT_INFO
           <no changes>
           VALUE "ProductVersion", STR_PROD_VER
<no changes>

With this setup, I could edit my build script (which uses Perl) to modify the version info in the VersionInfo.h file before rebuilding the entire project using the devenv command line.

One additional step that I added that may also be of interest (although it is not completely perfected yet, and may be a future question here) is to generate a unique build number every time the project is built. In the current incarnation, it always works for complete rebuilds, but only sporadically on incremental builds. What I did was create a file called build_number.incl that contains the following:

#define CurrentBuildNumber  "20081020P1525" 

Which is essentially the date and time that the build was started. I created a batch file that is run as a pre-build event to the project that generates this file. The script also defines BuildYear so that the copyright in the VersionInfo.h file always contains the year of the most recent build. The batch script is the following:

    echo Generating Build Number
    @For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @(
    Set Month=%%A
    Set Day=%%B
    Set Year=%%C
    )

    @For /F "tokens=1,2,3 delims=/M: " %%A in ('Time /t') do @(
    Set Hour=%%A
    Set Minute=%%B
    Set AmPm=%%C
    )

    @echo #define CurrentBuildNumber  "%Year%%Month%%Day%%AmPm%%Hour%%Minute%" > "$(ProjectDir)\build_number.incl"
    @echo #define BuildYear "%Year%" >> "$(ProjectDir)\build_number.incl"
    echo ----------------------------------------------------------------------

This file is then included in any file in the project that needs to use the build number (i.e. the about box).

Some of this was gleaned from this CodeProject post.

Hopefully this info proves helpful.

like image 25
bsruth Avatar answered Dec 24 '22 21:12

bsruth