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=”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:


    <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 (15)

  1. Raskal says:

    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).aspx

  2. deepak says:

    What is the difference between "<Content Include=…." and "<EmbeddedResource Include=…….." in .csproj file.

    How it is interpreted during build??

  3. Roee says:

    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 ?

  4. RuneFS says:

    @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!

  5. Asaf says:

    This only works with built-in project types, such as C#. With a project type created, say, with MPF it doesn’t work.

  6. Nngbsjbj says:

    Open this post and read what I think about that:,

  7. Semil says:

    <a href= http://index1.ideasor.com >east fork campground</a> <a href= http://index2.ideasor.com >edward vi and sex</a> <a href= http://index3.ideasor.com >bikini weight loss competition</a> <a href= http://index4.ideasor.com >illinois i-pass</a>