A question was posed to me this evening about copying and renaming Visual Studio projects. It is a lengthy enough topic to explain, I thought I better write it down. So here goes:
Renaming Projects
Step 1: Renaming projects can be an interesting challenge for the uninitiated. It's not quite as simple as just renaming the project node in the Solution Explorer tool window. Renaming the project node doesn't change the assembly name or the root namespace or the physical project folder name. Let's take a look at how to do just that.
When you rename a project, most likely you will want to rename the output assembly and/or the root namespace for the project. To do this, right-click on the project node and choose Properties. You can also double-click on the Properties (C#) or My Project (VB) node.
If it is not already selected, click on the Application tab. You will see two textboxes at the top; Assembly name and Root namespace. Assembly name is the filename given to the project's output assembly. A class library with an assembly name of Class1 will output a DLL named Class1.dll. Root namespace is, as the name suggests, the root namespace used for the project.
You may wish to change both the assembly name and root namespace when renaming a project.
Step 2: Next, I usually like to rename the physical project folder. To do this, close the solution within Visual Studio (you may have to close Visual Studio as well) and browse to the project folder. An easier way to get to the project folder might be to open a project file, right-click on the file's tab, and choose Open Containing Folder, then closing the solution or Visual Studio if necessary.
When you are at the project folder, rename it to match the new project name. Next, re-open the solution in Visual Studio. Visual Studio will complain that it can't load one or more projects.
This is fine, click OK. You will notice that your project is grayed out. Select the unavailable project node, go to the Properties tool window, and you will see that the File path property is editable. Click the value to show the ellipsis,
, and click the ellipsis button. The dialog should be in the solution path, so browse into the renamed project folder and select the project file (*.*PROJ). Now, right-click on the unavailable project node in the Solution Explorer tool window, and choose Reload Project. Now your project is renamed and re-integrated into the solution.
Renaming
Solutions
The process of renaming solutions is much easier. Simply rename the solution in the Solution Explorer tool window, and rename to physical solution folder. After renaming the solution folder, I would recommend opening up the solution file (*.SLN) manually while you are there as Visual Studio's MRU (Most Recently Used) shortcut lists won't be updated.
Copying Projects
Copying projects into the same solution requires a bit more legwork, including digging into the project files (*.*PROJ) themselves. First, go into Windows Explorer, and make a copy of the project folder, giving it the new project name.
Next, open up the solution file (*.SLN) in notepad. For our ClassLibrary1 solution, the solution file looks like:
Microsoft Visual Studio Solution File, Format Version 9.00
# Visual Studio 2005
Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "ClassLibrary1", "ClassLibrary1\ClassLibrary1.vbproj", "{9ED830D5-77A8-453D-B1CF-02E310E45EB1}"
EndProject
We are interested in the Project section. Copy those two lines, starting with Project and ending with EndProject. Then paste the text below the EndProject in a new line. If I wanted to add ClassLibrary1 as a new project called ClassLibrary2, I would change those references on the second project lines. The result would look like:
Microsoft Visual Studio Solution File, Format Version 9.00
# Visual Studio 2005
Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "ClassLibrary1", "ClassLibrary1\ClassLibrary1.vbproj", "{9ED830D5-77A8-453D-B1CF-02E310E45EB1}"
EndProject
Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "ClassLibrary2", "ClassLibrary2\ClassLibrary2.vbproj", "{9ED830D5-77A8-453D-B1CF-02E310E45EB1}"
EndProject
The final step is to go into the new project folder, ClassLibrary2, and rename the project file. In our case, to ClassLibrary2.vbproj.
That's it. Open Visual Studio, and enjoy.
Copying Solutions
Again, copying solutions is much easier. Simply copy the solution directory (the one containing the .SLN file, give it a new name, and rename the solution file. If you wish, you may also want to rename the solution node in the Solution Explorer tool window.
Conclusion
It is possible to rename and copy projects and solutions, though it is a very manual process. I think it would be helpful to have an option to sync directory names with project and solution names in Visual Studio. If I find the time, I would like to build a Visual Studio add-in that does this and takes care of a lot of these manual steps automatically and intuitively.