Based on your last comment on the original question, I would take a different approach and forget the approach you are currently using. You should be aware that your version information does not have to be in the AssemblyInfo.cs file. It can be in any code file if you have only the AssemblyVersion and AssemblyFileVersion attributes defined after each. With that said, what would I do, follow these steps:
- Remove AssemblyVersion and AssemblyFileVersion from AssemblyInfo.cs
- Create a new file, name it whatever you want, in my case I put it in Properties \ VersionInfo.cs. Do not add this file to the project.
- Edit the project file to include this file in the list of file that should be compiled only if you want it
Set the bit to # 3. When you create a .NET project, the project itself is an MSBuild file. Inside this file you will find the element declared by compilation. This is a list of files that will be sent to the compiler for compilation. You can dynamically include / exclude files from this list. In this case, you want to include the VersionInfo.cs file only if you are building a build server (or some other condition that you define). In this example, I determined what condition should be if the project was built in Release mode. Thus, for Release mode, VersionInfo.cs will be sent to the compiler, but for other assemblies it will not. Here is the contents of VersionInfo.cs
VersionInfo.cs
[assembly: System.Reflection.AssemblyVersion("1.2.3.4")] [assembly: System.Reflection.AssemblyFileVersion("1.2.3.4")]
To associate this with the build process, you must edit the project file. In this file you will find an element (possibly more than 1 depending on the type of project). You must add a goal similar to the following.
<Target Name="BeforeCompile"> <ItemGroup Condition=" '$(Configuration)'=='Release' "> <Compile Include="Properties\VersionInfo.cs" /> </ItemGroup> </Target>
Here is what I did here to define the goal, BeforeCompile, which is a well-known goal that you can override. See Article
Since we know that TeamFoundationServerUrl is only defined when created through TFS.
If you create a command line form, then something like this
<Target Name="BeforeCompile"> <ItemGroup Condition=" '$(IncludeVersionInfo)'=='true' "> <Compile Include="Properties\VersionInfo.cs" /> </ItemGroup> </Target>
And when you build the project, just do msbuild.exe YourProject.proj / p: IncludeVersion = true. Note: when building a solution, this will not work.