How to update the build number in a .RC file every time I build?
I thought this was such a basic thing that it would be built into VS, or answered in a FAQ somewhere, but I can't find the answer. What is the best way to increment the build number of my (non-managed) C++ exe
I went searching a while back for a way to do this also, but found most solutions were macros within Visual Studio, which is obviously not ideal when building with MSBuild.
So I wrote a simple command-line application that simply modifies an rc based on parameters passed to it. It wouldn't be too hard to write a task to do the same thing.
Here is an example that does it:
string tempPath = Path.GetTempFileName();
using
(StreamReader reader = new StreamReader(inputPath, System.Text.Encoding.Default)) { using (StreamWriter writer = new StreamWriter(tempPath, false, reader.CurrentEncoding)) {
string line;
while ((line = reader.ReadLine()) != null) {
if (line.StartsWith(" FILEVERSION")) {
line = string.Format(CultureInfo.InvariantCulture, " FILEVERSION {0},{1},{2},{3}", version.Major, version.Minor, version.Build, version.Revision);
} else if (line.StartsWith(" PRODUCTVERSION")) { line = string.Format(CultureInfo.InvariantCulture, " PRODUCTVERSION {0},{1},{2},{3}", version.Major, version.Minor, version.Build, version.Revision); } else if (line.StartsWith(" VALUE \"FileVersion\"")) {
line = string.Format(CultureInfo.InvariantCulture, " VALUE \"FileVersion\", \"{0}\"", version.ToString()); } else if (line.StartsWith(" VALUE \"ProductVersion\"")) { line = string.Format(CultureInfo.InvariantCulture, " VALUE \"ProductVersion\", \"{0}\"", version.ToString()); }
writer.WriteLine(line); } } }
File.Copy(tempPath, outputPath, true);
Sorry for the hard to read code but these forums are terrible at formatting code.
Microsofts internal build process uses a system very similar to the one described in the articles posted.
Since our processes are based around a "daily build" the version information that is stamped into DLLs and EXEs is controlled centrally and corresponds to the day that the build was done.
We have a header file (used by RC files amongst others) that is generated by our build lab and checked into the tree. All DLLs and EXEs then just include a standard .RC that includes this header and gets the file information. Interestingly, here in the developer division we have two: one that represents the .NET Framework version and one that represents the Visual Studio version. Components then include the appropraite one.
This process is exactly the same when using MSBuild and VCBuild.
The problem with the links you provided is that are require that you build using Visual Studio. Therefore if you are going to use MSBuild to build your applications in the future, that's not going to work.
How to update the build number in a .RC file every time I build?
jefri besharati
MORRTI
PorkyPete
I went searching a while back for a way to do this also, but found most solutions were macros within Visual Studio, which is obviously not ideal when building with MSBuild.
So I wrote a simple command-line application that simply modifies an rc based on parameters passed to it. It wouldn't be too hard to write a task to do the same thing.
Here is an example that does it:
string tempPath = Path.GetTempFileName();
using
(StreamReader reader = new StreamReader(inputPath, System.Text.Encoding.Default)){
using (StreamWriter writer = new StreamWriter(tempPath, false, reader.CurrentEncoding))
{
string line;
while ((line = reader.ReadLine()) != null)
{
if (line.StartsWith(" FILEVERSION"))
{
line = string.Format(CultureInfo.InvariantCulture, " FILEVERSION {0},{1},{2},{3}", version.Major, version.Minor, version.Build, version.Revision);
}
else if (line.StartsWith(" PRODUCTVERSION"))
{
line = string.Format(CultureInfo.InvariantCulture, " PRODUCTVERSION {0},{1},{2},{3}", version.Major, version.Minor, version.Build, version.Revision);
}
else if (line.StartsWith(" VALUE \"FileVersion\""))
{
line = string.Format(CultureInfo.InvariantCulture, " VALUE \"FileVersion\", \"{0}\"", version.ToString());
}
else if (line.StartsWith(" VALUE \"ProductVersion\""))
{
line = string.Format(CultureInfo.InvariantCulture, " VALUE \"ProductVersion\", \"{0}\"", version.ToString());
}
writer.WriteLine(line);
}
}
}
File.Copy(tempPath, outputPath, true);
Sorry for the hard to read code but these forums are terrible at formatting code.
Barrie
Microsofts internal build process uses a system very similar to the one described in the articles posted.
Since our processes are based around a "daily build" the version information that is stamped into DLLs and EXEs is controlled centrally and corresponds to the day that the build was done.
We have a header file (used by RC files amongst others) that is generated by our build lab and checked into the tree. All DLLs and EXEs then just include a standard .RC that includes this header and gets the file information. Interestingly, here in the developer division we have two: one that represents the .NET Framework version and one that represents the Visual Studio version. Components then include the appropraite one.
This process is exactly the same when using MSBuild and VCBuild.
Automating the generation of the header file using MSBuild would be very straightforward - you could create a custom task that creates the file. Here's a link to how to create a custom task: http://channel9.msdn.com/wiki/default.aspx/MSBuild.ExtendMSBuildWithANewTask
Hope that helps,
Kieran Mockford
MSBuild Team.
John Samperi
Michael B. Price
skinnybit
How To Increment Version Information After Each Build in Visual C++
http://support.microsoft.com/kb/q237870/
(at least for VC6)
For VC2002/2003 you can find an example here:
http://www.codeguru.com/Csharp/.NET/net_vs_addins/article.php/c5961/
Currently I am not aware of an build-in build-number inrementer in VC2005 Beta2...