Copy and Rename of C# .NET Visual Studio projects

29/05/2013 R Tyler 0 Comments

I had to rename a Visual Studio Project recently. This wasn’t as straightforward as I thought it would be. After a first failed attempt I decided to hit the usual favourite forums. (I had done a project rename successfully years ago but had long since forgotten what I did on that occasion)… so studying a few articles on the subject here’s one method I came up with which you can use if you so wish. I consulted two existing articles on this topic [references below] prior to assembling this list. These were for Visual Basic projects. So this is my take on the procedure for C# (I hope it’s of some use to you reader) – The steps are very similar:

1. Close all Visual Studio Projects.

2. Make a copy of the original project and store elsewhere (in case of problems).

3. Use the copied folder as the new source folder. Below this source folder will be the ‘tree’ of associated folders (i.e. your project sub-folders)

4. Assuming the project is called ‘OldProject’ the following file/folder structure should be in place:

            [root folder]            \OldProject
					OldProject.sln
					OldProject.suo  [ delete this file]
	    [root sub-folder]        \OldProject\OldProject
				     \OldProject\OldProject\App_Classes
				     \OldProject\OldProject\... etc ...
    
Rename the above folders to your new chosen name (as shown below). You can safely do this without damaging the project (besides you will have your backup stored of course!)

            [root folder]   	    \NewProject
				        NewProject.sln

	    [root sub-folder]	    \NewProject\NewProject
				    \NewProject\NewProject\App_Classes
				    \NewProject\NewProject\...etc...
    

5. Open the new root sub-folder and rename the ‘OldProject.csproj’ file within it to ‘NewProject.csproj’

6. Open Visual Studio

7. Select the File| Open Project… menu option and in the dialog presented search for and select the new solution (.sln) file.

8. The following error message will be given:


    "One or more projects in the solution were not loaded 
     correctly - Please see Output Window for details"

9. Just ignore this message and Click [OK] to Proceed

10. When the Project opens, Right-Click on the Project in the Solutions Explorer, and select ‘Remove’.

11. Again, Right-Click in the Solutions Explorer, and select Add | Existing project…

12. Firstly open the ‘Microsoft Visual Studio Solution’ file and rename any old project reference to the new project reference. Next, point to, and open, the new (.vbproj or .csproj) file in the app (root)\(sub) folder. The project should now load correctly. However the project files will still retain references to the OldProject string. Using Edit | Find and Replace | Replace in Files, replace all references to ‘Oldproject’ with ‘NewProject’. In my case there were 29 files with replaced strings.

13. Next, Right-Click the project root folder and select the last item on the list ‘Properties’. In the field placeholders shown for “Assembly name:” and “Default namespace:”, enter the new project name. Then click File | Save All.

14. Use ‘Build | Rebuild’ to rebuild the project with the changed name. Assuming the old Project was able to compile, the new Project should also compile and create the associated build files.

15. Try out the newly named project using the usual ‘Run’ menu option (or Ctrl F5 keys). The newly named project should run as expected. There is one further additional step (for clean up of the old project files (if required)). In my case I found this wasn’t necessary.

16. Close the application and Visual Studio. Navigate to the folder ‘.\NewProject\NewProject\obj\Debug’ and delete any files with the old project name.

Note: The .sln file contains the reference to the .vbproj file, so you may need to open it (in notepad) and change the header information that reads:


  Microsoft Visual Studio Solution File, Format Version 11.00
  # Visual Web Developer 2010
  Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = 
  "OldProject", "OldProject.csproj",
  "{9AF311CD-44C8-44E4-800E-394F458361A5}"
  EndProject


Just replace all the instances of ‘OldProject’ with ‘NewProject’. That concludes the procedure. If you do have any questions please drop me a line. Google

Leave a Reply: