How to customize your MSBuild project file manually


To hand-modify your managed project file, right click on the project, choose Unload, right click again, choose Edit. After editing (with intellisense!) save and close. Right click on the project, choose Reload. (If you are using Beta 2, after unloading the project, do File>Open>File and choose the project file manually.)

As well as modifying properties and items in this editor, you can even add steps to the build. For example, to generate a new file during the build and get it compiled into the assembly, you could add this just before the closing </Project>

<!– Overriding the BeforeCompile target to insert this step just before the compilation step –>
<Target Name="BeforeCompile">
<!– Use Exec task to create a code file; this could be done more elegantly with a custom task–>
<Exec Command="echo static class BuildMachine { public static string Name { get { return &quot;%COMPUTERNAME%&quot;; }}} &gt; buildMachine.cs" Outputs="buildMachine.cs">
<!– Add this file to the list to compile –>
<Output ItemName="Compile" TaskParameter="Outputs" />
</Exec>
</Target>

Now you can use BuildMachine.Name in your project to programmatically get the name of the machine that built the assembly. You could have achieved this specific example in VS 2003 by creating the file in a pre-build event, or using a compiler define, but there is no way in VS 2003 you could have hooked into the middle of the build process as described above. A more elegant use would be to use a custom task to modify your AssemblyInfo.cs file during the build, to increment the AssemblyVersion attribute.

More info on MSBuild is here https://channel9.msdn.com/wiki/default.aspx/MSBuild.HomePage

Happy Visual Studio’ing!

Comments (3)

  1. Norman Diamond says:

    That functionality looks like it’s just begging to have an Edit entry in its right-click menu all the time. If the project is currently loaded then display a message box saying "Visual Studio will temporarily unload the project to let you edit the file, and will reload it when you’re done. [OK] [Cancel]".

    The traditional way to do this was to quit Visual Studio, fire up Emacs or Vim or Notepad, and then start up Visual Studio again. It’s nice to see improved functionality. The way to make improved functionality visible to users is, besides a dedicated pipe between your blog and every end user’s executing instance of Visual Studio, is to make them always visible in the right-click menu.

  2. Dan says:

    Norman, you’re totally right that this command ought to be visible all the time. (And it should automatically unload the project when you Edit, and automatically reload the project when you close the file. And reloading the project should re-open the files you previously had open.) This scenario probably didn’t receive the attention it deserved until quite late in the cycle, when there was limited time. I would like it to get much smoother next version.

    Thanks for your feedback. You can give other feedback at my team’s email below.

    Dan (msbuild@microsoft.com)

  3. Sarika says:

    Why IDE Customization Event Handlers like BuildEvents_OnBuildDone() doesn’t give access to the project file name which is actually built at last(after all dependent projects are built) ?

    I have DeklarIt Tool Installed in IDE, when Deklarit project is built (has an extension of .dklp), it no where is showing the project name from parameters provided by IDE Build Customization Events.