TFS, add the build number into the code as variable. RRS feed

  • Question

  • Hi, I'm new to using TFS like to be able to get the build number from the info in our app, so we can show it in the help section in the app. It's formatted like this $(date:yyyyMMdd)$(rev:.r)_$(SourceBranchName). But how do I add a variable in the code, and have it populated during the build by TFS. I assume I add it in the "process variables" section. But how? and how too call it in the code.


    Tuesday, May 1, 2018 1:06 AM

All replies

  • It's where you may have to post, since MSDN forum has no traffic.


    Tuesday, May 1, 2018 2:00 AM
  • I'm afraid that isn't going to work. A process variable is for getting variables within a build, not after the build. The build number may or may not be what you want to show to the end user. Unless it is part of your versioning processing then I wouldn't even bother. If it is part of your versioning process then it would be in the version info for the assembly so you don't care where it came from. You can also change the build number format that TFS uses if you'd like to make it more closely match your versioning processing.

    If you really want to use the build number in code then it has to be stored in the code somewhere. The easiest way to do that would be to create a source file that exposes it as a const value.

    public static class VersionInformation
       public const string BuildNumber = "";
    TFS won't update code so you then need to write a build task to find this file and update the variable accordingly. There are quite a few third party build tasks written that can substitute values in files so you can use one of them. After grabbing the code but before the build you'd run the replacement task to inject the build number into the class. It is now accessible during compilation so you can reference it in your code.

    Michael Taylor http://www.michaeltaylorp3.net

    • Proposed as answer by Fei Hu Wednesday, May 2, 2018 10:02 AM
    Tuesday, May 1, 2018 1:53 PM