Automatically Updating .SLN files to VS 2012

Visual Studio 2012 finally gave us backwards project compatibility so you can slide between VS 2010 and VS 2012 as you needed. It’s a great feature and one we’ve sorely needed for a long time. When I upgraded my projects to .NET 4.5 or the new C++ compiler, I did so inside the IDE. That took care of the projects, but I noticed the .SLN files were still in the older format. That wasn’t too much of a concern because I normally open .SLN files from inside the IDE. In those rare cases where I used Explorer to double click on the .SLN, I cursed as I waited for VS 2010 to start. Once you get used to VS 2012, it’s always surprising to see how fast it is in comparison to VS 2010. After one too many wrong opens I wanted to get any lingering .SLN files updated to VS 2012 once and for all. As I’m probably not the only one needing to update .SLN files, I thought I’d post for others.

Thinking I could automate the conversion with the /upgrade switch with DEVENV.EXE, I headed over to PowerShell and at the top of my project tree ran the following one liner (note that Visual Studio is in my path)

  1. dir -Recurse *.sln | ForEach-Object { devenv /upgrade $_.FullName}

That command took a while grinding away and according to the Pending Changes view in Visual Studio changed a lot of files. I thought that would only change .SLN files since my individual .CSPROJ and .VCXPROJ files were already upgraded. Looking at the differences I noticed the .VCXPROJ files (C++ projects) in particular had a lot of changes that didn’t look good. As I suspected, the /upgrade switch changed some dependencies on projects related to differences between 32-bit and 64-bit. That would have broken my build so I quickly hit the undo and set about a different approach to the changes.

With my projects currently building and working correctly, all I wanted to do was change the .SLN files to open in VS 2012 instead of VS 2010. If you compare the different .SLN files it’s nothing more than substituting an 11 for a 12 in the Format Version and updating the Visual Studio version to 2012.

That’s simple enough but I wanted to automate it and since PowerShell is my automation language of choice, that should be simple to do. Well, not quite so simple. I need to read in the text of the .SLN, modify it, and write it out. PowerShell doesn’t support that directly, but a quick search turned up that the prolific Bill Stewart had already created a nice PowerShell script I’d used in the past, Replace-FileString.ps1. Since Bill did all the work, I just chained it together into a two-liner since I needed to replace two different strings.

  1. dir -Recurse *.sln | Replace-FileString.ps1 -Encoding UTF8 -Pattern “Format Version 11.00” -Replacement “Format Version 12.00” -Overwrite
  2. dir -Recurse *.sln |Replace-FileString.ps1 -Encoding UTF8 -Pattern “# Visual Studio 2010” -Replacement “# Visual Studio 2012” -Overwrite

Hope you find this useful. You should double and triple check everything on your systems as you can break things if you are not careful.

We deliver solutions that accelerate the value of Azure.

Ready to experience the full power of Microsoft Azure?

Start Today

Blog Home

Stay Connected

Upcoming Events

All Events