Click here to monitor SSC

Simon Cooper

Modifying built-in Visual Studio project templates

Published Tuesday, August 17, 2010 8:45 PM

As some of you may know, executable projects created in Visual Studio 2010 now default to the x86 build target rather than AnyCPU, as was the case in VS2008. I personally am quite annoyed by this decision, and so I looked for ways to change it back. A poke through the VS options screen proved fruitless, however I was able to modify the Visual Studio templates themselves to affect this change. This post explains how to do it.

Modifying built-in templates

All the project templates in the Visual Studio 'New Project' screen are stored in zip files on disk; on my computer, these are located at C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ProjectTemplates. The templates themselves are in subfolders; the 'Windows Forms Application' and 'Console Application' templates I wanted to edit were in the CSharp\Windows\1033 subdirectory.

Editing these are fairly simple, as they have the same structure as user project templates. All the needed .csproj and .cs files are included in the zip, along with a .vstemplate file that tells Visual Studio what to do with each file. To edit these files you'll need to extract the zip, edit the files, and then remove/rename the old zip file and replace it with a new zip file containing the edited files.

x86 -> AnyCPU

To change the default target from x86 to AnyCPU, edit the .csproj file and change references from 'x86' to 'AnyCPU' - there should be a Platform xml element, two PropertyGroup conditions, and two PlatformTarget elements (these can be removed completely).

You can also make any other changes you wish; as well as changing the console and windows forms project targets back to AnyCPU, I removed everything but System.dll from the assembly references, and removed the Class1.cs file from the Class Library template (note that if you're removing files you'll have to remove references from the .vstemplate file as well as the .csproj file).

Re-creating the template cache

Once you've made all the changes you want, and overwritten the zip files with the new copies, you need to delete the contents of the Common7\IDE\ProjectTemplatesCache directory, and run devenv /installvstemplates in a Visual Studio command prompt, which will re-create the cached templates using your versions. The new templates will then be used for any subsequent created projects.

Comments

 

qwertie said:

I can understand why they did this. I used to develop on a 32-bit machine and I used P/Invoke extensively on 32-bit DLLs and everything worked fine. I never occurred to me that my .NET code was broken on all 64-bit machines because it was set to AnyCPU. Defaulting to x86 fixes that problem (and it's hard to complain about the memory savings you get from a 32-bit process--well, at least for me. I like savings!) I sure hope .NET allows "x86" assemblies to be loaded by exe files that are set to "AnyCPU", otherwise Microsoft just traded one set of incompatibility problems with another.
April 12, 2011 4:33 AM
You need to sign in to comment on this blog

About Simon Cooper

Simon joined Red Gate as a software developer in 2007. He started off in the SQL Tools division, working on SQL Compare 7 and 8, moved onto Schema Compare for Oracle, and is now in the .NET division working on SmartAssembly.
Latest articles
A first look at SQL Server 2012 Availability Group Wait Statistics
 If you are trouble-shooting an AlwaysOn Availability Group topology, a study of the wait statistics... Read more...

SQL Server Prefetch and Query Performance
 Prefetching can make a surprising difference to SQL Server query execution times where there is a high... Read more...

SSIS Basics: Setting Up Your Initial Package
 When working with databases, the use of SQL Server Integration Services (SSIS) is a skill that often... Read more...

Checking Out SQL Backup Pro 7’s New Automatic Backup Verification
 Wouldn't it be great to offload the daily chore of checking the integrity of your production... Read more...

Chuck Lathrope: DBA of the Day
 Chuck Lathrope was a finalist for the Exceptional DBA of the Year award in 2009. We contacted him to... Read more...