So far on this blog we’ve shown different ways can use MSBuild to tweak behavior from the command line, but never really talked about things you can do with MSBuild that will affect the Visual Studio user interface. Since managed projects in Visual Studio use the MSBuild file format there’s some neat things you can do in targets files that will cause things to change in the IDE.
Last week Prasadi (who works on the project system) was digging into some stuff for the WinFx team. In the process Kieran wound up showing us how to add a new build action to the following drop-down in Visual Studio:
While it wasn’t what the WinFx team needed, I thought it was pretty darn cool so I figured I’d share it with everyone. Even better, it’s incredibly easy and done through MSBuild files. Here’s how to do it:
1. Create a custom .targets file.2. Put the following inside it:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <ItemGroup> <AvailableItemName Include="MyBuildAction" /> </ItemGroup> </Project>
3. Import the new .targets file into any project you want to have use the new build action:
That’s all there is to it. Now your special build action will show up in the dropdown inside Visual Studio:
You must admit, that’s pretty slick. The real question is “what good is it?” Well, when you select something from the Build Action dropdown it controls the list the file is added to in the project file. So, for example, if you change the build action for Form.cs to MyBuildAction the following will show up in the project file:
<ItemGroup> <MyBuildAction Include="Form1.cs"> <SubType>Form</SubType> </MyBuildAction><ItemGroup>
You can then use the MyBuildAction list as an input to any custom target you might like.
[ Author: Neil Enns ]