How To Clone a Solution in Visual Studio
In short, you can't. Well, not without using some sort of Version Control system like Git.
(UPDATE: I've explained in more detail how you can create your own cloner tool.)
I don't use Git. I do know how to use Git. I just don't use it. I'm odd that way, I know.
I'm a lone 'hobbyist' coder, who programs in various languages and IDEs for fun. I've released some of my software over the years for others to use (and some people even paid me money for licences!). The most popular software I've written currently is my Gopher Browser for Windows, which seems to be well-received by the gopher-using community.
Anyway, back on topic... I write a lot of small utilities, mainly to solve personal problems I encounter, and I find myself constantly having to copy/paste routines out of Solution 'A' into Solution 'B'. Sometimes I even want to use an older codebase as a starting point for another tool.
Visual Studio (I use 2019 currently, but 2022 is the same) doesn't seem to offer a native way to clone a solution or project. At least, I can't find how it's done, and I'm not the only one, based on a bit of Googling. As I wanted this functionality, I did what any self-respecting coder would do; I wrote a tool to do it myself:
It's not very complicated, all it does is:
- Takes a command line parameter to auto-populate the Source Solution Folder textbox.
- Copy the complete solution directory tree from the source location to the target.
- Excludes the \obj\ subfolders.
- Includes the \bin\ subfolders, as sometimes there are settings files or support data in there.
- Renames any reference of the original Solution name to the new Solution name (based on the containing target folder name).
- Unless asked not to, it regenerates the main Solution GUID, and the Project GUIDs so the clone is 'unique'.
To integrate into the Visual Studio 'Tools' menu as an External Tool, the following needs to be done:
- On the Tools menu, select 'External Tools'
- In the dialog shown, give the new tool a Title. I went with 'Clone Solution'.
- For 'Command', enter the complete path to the tool's binary.
- For 'Arguments', enter the variable Visual Studio uses for the current Solution folder which is: $(SolutionDir)
Once that's done, the Clone tool can be launched directly inside Visual Studio, and I can clone Solutions easily.
It's not perfect; It only supports VB and C# solutions and only one project per solution (I rarely have more than one project in a solution, so not a priority feature for me), but for re-using existing solutions as the base for a new tool or application, it's going to make my workflow faster, and that's what counts right?