How To: Add a custom build action to Visual Studio
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="https://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:
<Import Project="d:\temp\neil.targets.xml"/>
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 ]
Comments
Anonymous
October 28, 2005
It's a fantastic tips. Unfortunately, the buid which I had (September CTP) doesn't seem to support this tips.
The result with my configuration is indeed the addition of an action named "AvailableItemName" and the addition of an item named MyBuildAction in my project files.
Ref : http://msdn2.microsoft.com/en-us/library/ms171468(en-US,VS.80).aspxAnonymous
November 04, 2006
http://www.blogging.co.uk/christmasgifts <a href="http://www.blogging.co.uk/christmasgifts">christmas gifts</a> christmas giftsAnonymous
November 18, 2006
http://www.foren-dienst.de/cgi-bin/forenserver/foren/F_9999/cutecast.pl <a href="http://www.foren-dienst.de/cgi-bin/forenserver/foren/F_9999/cutecast.pl">christmas gifts</a> christmas giftsAnonymous
March 25, 2007
PingBack from http://www.marcosdellantonio.net/blog/index.php/2007/03/26/net-propriedades-dos-arquivos/Anonymous
May 04, 2007
What is the difference between "<Content Include=...." and "<EmbeddedResource Include=........" in .csproj file. How it is interpreted during build??Anonymous
July 06, 2007
It worked great, but now I've encountered a new problem. the items with my new custom build action are not copied to output folder although I set them as 'Copy Always' in the output to target directory property. Does anyone know how to fix that ?Anonymous
April 09, 2008
@deepak: if they correspond to the targets Content and Embeded resource in VS then it goes like this: When choosing the Content target the file it self is part of the output of the process (it's copied to the compiler destination) When choosing the embed option the content of the file is included in the assembly as a binary resource and can be acces with code similiar to GetType().GetAssembly().GetManifestResourceStream(name) where name usually is the default namespace of the project + . + name of file including extension. The filename part is case sensitive!Anonymous
April 13, 2008
This only works with built-in project types, such as C#. With a project type created, say, with MPF it doesn't work.Anonymous
June 15, 2008
Open this post and read what I think about that:,Anonymous
December 10, 2008
The comment has been removedAnonymous
June 07, 2009
PingBack from http://greenteafatburner.info/story.php?id=2144Anonymous
June 13, 2009
PingBack from http://gardendecordesign.info/story.php?id=3483Anonymous
June 13, 2009
PingBack from http://gardenstatuesgalore.info/story.php?id=1506Anonymous
June 18, 2009
PingBack from http://outdoordecoration.info/story.php?id=2391Anonymous
June 18, 2009
PingBack from http://patioumbrellasource.info/story.php?id=1173