Mostly, It's All About Tech... Mostly.

How To Clone a Solution in Visual Studio (Part 2)

The original post on cloning a solution in Visual Studio seems to attract a lot of views, so I guess this is a fairly hot topic for some people. I feel a little bad when I see how many people are viewing that post because I hadn't actually explained how to do it.

This follow-up post will explain what you need to do to clone a solution in Visual Studio if you are not using a source control repository such as Git.

As I said previously, these steps are for solutions with one project within them. This fits my needs as I very rarely write anything big enough to require a multi-project solution. Although I am (still) using Visual Studio 2019, these instructions should be valid for Visual Studio 2022 and beyond.

Step 1: Replace the GUIDs in the Solution file.

Each Visual Studio solution starts with a .sln solution file. This file is a text-based meta-data file that includes details that inform Visual Studio on where to find the project files for the solution, what version of Visual Studio it was last saved from, and the default build targets (among other things).

The first line we are interested in is the Project line. This line starts with 'Project(' and contains two GUIDs, the first one, which I've called 'MainGUID' and a second one, which I've called 'ProjectGUID'.

What we need to do is replace these GUIDs with new fresh GUIDs, using the Guid.NewGuid function.

The next line we're interested in starts with 'SolutionGUID = ', and unlike the Project line, it is prefixed by two tabs. However, it's easier to parse as it's only got one GUID after the equals sign. Again, we need to replace this GUID with a new one.

By replacing all the GUIDs in the Visual Studio solution file, you are effectively telling Visual Studio that this is a whole new solution with new projects attached. You don't have to do this; the clone will still work. My tool has the option to skip it, and I do it on all clones by default, just to be safe.

Step 2: Renaming the Solution and Project

We already know the current solution name, as it's the name of the solution (.sln) file we are parsing, so to change the name of the solution (and the project name, which in my use case is always the same; your use case may be different), we just search and replace the old name for a new name in the solution file as a whole, simply by using 'string.replace(oldname,newname)'.

We then save out the solution file to its new location, renaming it to the new solution name we've chosen so that it is now called 'new-location\new-name.sln'.

Step 3: Copying the Project Files

Now it's just a case of recursively copying all the files from the original solution location (and subfolders) to the new location. I exclude any files in or below the 'obj' folder, as these are transitory and not required for our cloned solution. I do include the 'bin' folders as sometimes support files are placed there (a bad practice, but it does happen).

I also do a search and replace on any project files that have these extensions:

.xml, .sln, .vb, .vbproj, .cs, csproj

(If you write code in other languages, you should add your code languages file extensions to this list)


So, to summarise, to clone a solution in Visual Studio, you need to:

  1. Copy all files except object files to the new location.
  2. Rename the solution file (.sln) to the solution's new name.
  3. Rename the project file(s) (.csproj or .vbproj) to the new name.
  4. Replace all the GUIDs in the new solution (.sln) file.
  5. Replace any reference to the old name with the new name in the solution (.sln) file.
  6. For all other text files in the solution, look for, and replace, the old name with the new name.

I'm not going to provide a link to my Visual Studio Solution Cloner as to be honest, anyone who knows how to code should be able to easily write a proof of concept based on the information above. Also, my coding style is rubbish, and no one should be subjected to my source code.