Visual Studio: default build action for non-default file-types

July 2, 2010 at 11:12 AMAndre Loker

With a simple file you can tell Visual Studio to use a certain default Build Action for specific file types. This reduces the chance of missing files on the server when using the built-in publishing functionality. 


Updated: if you don’t feel like hacking pkgdef files manually, check out my little helper tool at

For each file in the solution you can define a build action by selecting the file and choosing the appropriate “Build Action” in the properties window.

The build action defines how Visual Studio handles the file when building and publishing the solution. For example, the default build action for C# source file is “Compile”. Files that have no special meaning for Visual Studio get the build action “None” by default, telling VS to basically ignore the file and just leave it as is.

In an ASP.NET MVC project that uses a view engine different from the default one (such as Spark or Brail) one will frequently add such “meaningless” files to the solution, namely the view files: .spark, .brail, .brailjs etc.

Publishing in Visual Studio

This section only provides the context of this article. You may skip it if you know about publishing.

deploy Visual Studio offers a nice publishing function that makes publishing applications rather easy (Build -> Publish). The Web Deploy method is pretty cool, because it can deploy the project to a remote web server with one button click.

The publishing functionality can be configured in the project properties, tabs Package/Publish Web (and Package/Publish SQL for that matter).

One essential option is the choice of the “Items to deploy”. There are three choices: “Only files needed to run this application”, “All files in the project” and All files in the project folder. “All files in the project directory” publishes, well, all files in the directory where the project files lives and all subdirectories. “All files in the project” only deploys files that are part of the solution, that is, all files shown in the solution explorer (with “Show All Files” off, of course), plus the build results. publishAs a result, this will copy among other things all source files to the web server. It is not much of a security problem, because by default IIS is configured to not serve those files at all. Still, I don’t see the need to publish unnecessary files. That’s where the first option comes in: “Only files needed to run this application”. It basically deploys all compilation results and all files with the “Content” Build Action defined. For many file types, VS sets the Build Action to Content by default, for example .css, .js and .config files. For “unknown” file types, such as .spark or .brail, the Build Action is “None”. As a result, those files are not deployed to the server on publish with the “Only files needed to run this application” setting on. Of course, you can change the Build Action for each and every file, but it is a tedious task and can easily be forgotten, causing missing files on the server. Luckily, you can tell Visual Studio to use a different default Build Action for any file type.

Changing the default Build Action for a file-type

The default build action of a file type can be configured in the registry. However, instead of hacking the registry manually, we use a much better approach: pkgdef files (a good article about pkgdef files). In essence, pkdef are configuration files similar to .reg files that define registry keys and values that are automatically merged into the correct location in the real registry. If the pkgfile is removed, the changes are automatically undone. Thus, you can safely modify the registry without the danger of breaking anything – or at least, it’s easy to undo the damage.

Finally, here’s an example of how to change the default build action of a file type:

   1: [$RootKey$\Projects\{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}\FileExtensions\.spark]
   2: "DefaultBuildAction"="Content"

The Guid in the key refers to project type. In this case, “{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}” means “C# projects”. A rather comprehensive list of project type guids can be found here. Although it does not cover Visual Studio 2010 explicitly, the Guids apply to the current version as well. By the way, we can use C# as the project type here, because C# based MVC projects are in fact C# projects (and web application projects). For Visual Basic, you’d use “{F184B08F-C81C-45F6-A57F-5ABD9991F28F}” instead.

$RootKey$ is in abstraction of the real registry key that Visual Studio stores the configuration under: HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\10.0_Config (Note: Do not try to manually edit anything under this key as it can be overwritten at any time by Visual Studio).

The rest should be self explanatory: this option sets the default build action of .spark files to “Content”, so those files are included in the publishing process.

All you need to do now is to put this piece of text into a file with the extension pkgdef, put it somewhere under %PROGRAMFILES(x86)%\Microsoft Visual Studio 10.0\Common7\IDE\Extensions (on 64-bit systems) or %PROGRAMFILES(x86)%\Microsoft Visual Studio 10.0\Common7\IDE\Extensions (on 32-bit systems) and Visual Studio will load and apply the settings automatically the next time it starts. To undo the changes, simply remove the files.

Finally, I’ve attached a bunch of pkgdef files that are use in production that define the “Content” default Build Action for C# and VB projects for .spark, .brail, .brailjs and .less files respectively. Download them, save them somewhere in the Extensions folder and you’re good to go.

brail - Content.pkgdef (528.00 bytes)

brailjs - Content.pkgdef (534.00 bytes)

less - Content.pkgdef (525.00 bytes)

spark - Content.pkgdef (528.00 bytes)

Posted in: Visual Studio

Tags: , , ,

Missing header, footer and page number templates in Office 2007

November 13, 2009 at 11:53 AMAndre Loker

Office 2007 seems to get confused if you install a language version different from the language Windows uses. I recently installed an English version of Windows 7 after having used a German version of Vista for two years. When I installed Office 2007 (German) on Windows 7 I realized that several gallery items in Word were missing:

noheader nopagenumber

I reinstalled Office without success. On the web I found some forum threads (e.g. here) describing the same issue but the most common solution was to delete a file called Building Blocks.dotx in %appdata%\Microsoft\Document Building Blocks\<some language id>. “some language id” is 1031 (German) in my case. However, deleting the file did not fix the issue. As predicted the file got recreated when Word was started, but the gallery items were still gone.

Out of the blue I guessed that maybe there was an issue with me using an English Windows and a German Office. So I copied the Building Blocks.dotx from the 1031 subfolder to a new folder named 1033 (which is the language code for English). And what can I say – it worked again!



Posted in: Tools | Windows

Tags: , , ,

Markup Preview

October 17, 2009 at 12:27 PMAndre Loker

If you’ve ever put a project on GitHub you probably know that a README file in the root folder is automatically formatted and displayed at the project page. GitHub supports different markups for this purpose, such as Markdown, RDoc, Textile or reStructured Text.

I realized that it might come in handy to have a preview of what such a file would look like when rendered to HTML while editing it. That’s what I created my little toy project Markup Preview for. It’s a pretty simply web app which allows you to enter your markup (currently only Markdown and Textile are supported) on the left pane and see the rendered result immediately (well, almost) at the right pane.

I know that there are plenty of online converters for Markdown and Textile. Anyway, this one was fun to do, plus it adds kind-of realtime preview which is not common for those converters.

Here’s what it looks like in action (rendering its own file):


I installed the app at so feel free to give it a try.

The project is open source and available at Feedback is appreciated.

Posted in: Projects

Tags: , ,

XmlSerializer and automatic collection properties

August 7, 2009 at 2:09 PMAndre Loker

I noticed an interesting detail in the way XmlSerializer handles collection properties. FxCop rule CA2227 suggest to make collection properties read-only, because XmlSerializer is treating collections differently as described here (scroll down to the “Note” box under “Overriding Default Serialization”). When I tried to serialize the following class, however, I got an error during serialization:

   1: public class SomeClass
   2: {
   3:   public SomeClass()
   4:   {
   5:     CollectionProperty = new List<int>();
   6:   }
   8:   public List<int> CollectionProperty { get; private set; }
   9: }

The error was something like:

System.InvalidOperationException: Unable to generate a temporary class (result=1).

error CS0200: Property or indexer 'SomeClass.CollectionProperty' cannot be assigned to — it is read only.

Erm, right, I made it read-only because FxCop suggested it, so what’s wrong here? The answer is that “making the collection property read-only” should be read as “don’t provide a setter of any visibility”, because technically a property with a private setter is still writable, albeit from within the class only, leading to the runtime error as described above. FxCop on the other hand will stop complaining as soon as the setter is made private. 

As soon as I changed the automatic property to a property with backing field without a setter the application ran fine:

   1: public class SomeClass
   2: {
   3:   private readonly List<int> collectionProperty;
   5:   public SomeClass()
   6:   {
   7:     collectionProperty = new List<int>();
   8:   }
  10:   public List<int> CollectionProperty
  11:   {
  12:     get { return collectionProperty; }
  13:   }
  14: }

Posted in: C#

Tags: , ,

[Screencast] Einrichten eines Entwicklungsservers Teil 4 – Trac als Service

July 18, 2009 at 11:05 AMAndre Loker

In der heutigen Folge werden wir tracd, den Trac-Dämon, als Windows-Dienst installieren, um nicht ständig am Server eingelogt sein zu müssen.


Posted in: Screencasts | DotNetGerman Bloggers | Tools

Tags: , , ,