Visual Studio Hack

This post is perhaps a little geeky and probably has a very limited target audience. Basically if you want to know how to easily share development projects between Visual Studio 2008 users and Visual Studio 2010 users then read on. If you don't then you might want to move along, there's nothing else to see here folks.

When it comes to Windows development and .NET in particular we're running a bit behind the times in our office. We are currently still developing against .NET framework version 3.5 and using Visual Studio 2008 (VS2008). I do have a license for Visual Studio 2010 (VS2010) though and recently installed it to see what it's like. The main thing I was interested in was trying out the VsVim extension which isn't available in VS2008. This basically allows you to use the Vim key bindings in the code editor window within the VS2010 IDE. So what I really wanted to do was to continue developing my .NET 3.5 projects using VS2010.

Easy, when you open up a VS2008 solution in VS2010 the import wizard converts the solution and projects to the VS2010 format. These changes are a little annoying given that backwards compatibility has been a trademark of Microsoft's in the past. But the updated projects still target the 3.5 framework so it's not a big issue. Or so I thought. The problem arises when you want to then open the projects back up in VS2008. The projects apparently work OK but the VS2010 solution files will not open. Given that I'm working in a development team where everybody else is still using VS2008 it seemed that VS2010 was going to be a no go until such time as everyone has upgraded.

As usual I was determined to avoid defeat. A few web searches and several articles later the consensus appeared to be that having 2 solution files was the way to go. One in the VS2008 format and one in the VS2010 format. You just have to remember that whenever any changes to the solution are required you have to update both versions. I hate duplicating work so I wasn't over the moon with this workaround. Plus what happens if it's one of the other developers who updates the solution file, I'd have to check every time I pulled in their changes and then work out what they'd changed so I could keep my VS2010 version in sync.

I did a little bit of digging on the web and also compared some VS2008 solution and project files with their upgraded VS2010 versions. With a little trial and error it seemed that the following changes were required to open VS2008 projects and solutions in VS2010.

In the solution file:

-Microsoft Visual Studio Solution File, Format Version 10.00
-# Visual Studio 2008

+Microsoft Visual Studio Solution File, Format Version 11.00
+# Visual Studio 2010

And in the project files:

-Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5"

+Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0"

So what I came up with was to have 2 scripts I could run to convert a solution from VS2008 to VS2010 and back again. I'm sure there are probably a number of different ways to achieve this. As I'm using Git Extensions on Windows for source control I have a wealth of core Linux commands available to me at the Git-Bash command line and so I decided to make use of them.

Basically the following 2 commands convert the VS2008 solution and projects to VS2010.

find . -name "*.sln" -print | xargs sed -i -b 's/Format Version 10.00/Format Version 11.00/g;s/# Visual Studio 2008/# Visual Studio 2010/g'
find . -name "*.*proj" -print | xargs sed -i -b 's/ToolsVersion="3.5"/ToolsVersion="4.0"/g'

Simply reversing the search and replace strings returns the files to their original state. So the only thing I need to remember to do is to run the second script to convert the project and solution files back to the VS2008 format before pushing any changes into our shared repositories. Pretty simple really.