Proper way to rename solution (and directories) in

2019-01-03 11:07发布

I have a rather involved Visual Studio solution (2010, but it shouldn't matter) that I need to rename.

I want to rename the folders to match the new solution name, but I can't figure out a way to refactor the folder names automatically, and going through every single project file will be painful.

Is there an official way to do this? Alternatively (and much less preferably), are there free tools to accomplish this?

17条回答
Emotional °昔
2楼-- · 2019-01-03 11:44

The only solution which works for me on Visual Studio 2013 in a WEB project:

Lets say I want to rename "project1" to be "project2". Lets say the physical path to my .sln file is: c:\my\path\project1\project1.sln

so the path to my .csproj file as well as the bin and the obj folders should be: c:\my\path\project1\project1\

  1. Open the solution in VS by double clicking the project1.sln file.

  2. In Solution Explorer, right-click the project (NOT the solution!!!), select Rename, and enter a new name.

  3. In Solution Explorer, right-click the project and select Properties. On the Application tab, change the "Assembly name" and "Default namespace".

  4. In the main CS file (or any other code files like Global.asax for example), rename the namespace declaration to use the new name. For this right-click the namespace and select Refactor > Rename enter a new name. For example:

namespace project1

4.1 In Solution Explorer, right-click the project (NOT the solution!!!), select Rename, and enter a new name.

  1. Make sure: the AssemblyTitle and AssemblyProduct in Properties/AssemblyInfo.cs are set to the new name ("project2").

1 [assembly: AssemblyTitle("New Name Here")] 2 [assembly: AssemblyDescription("")] 3 [assembly: AssemblyConfiguration("")] 4 [assembly: AssemblyCompany("")] 5 [assembly: AssemblyProduct("New Name Here")] 6 [assembly: AssemblyCopyright("Copyright © 2013")] 7 [assembly: AssemblyTrademark("")] 8 [assembly: AssemblyCulture("")]

  1. Close the Visual Studio.

  2. Delete bin and obj directories physically.

  3. Rename the parent folder and the source folder to the new name (project2):

In the example: c:\my\path\project1\project1

will be: c:\my\path\project2\project2

  1. Rename the SLN file name by right click on that SLN file forward by Rename.

  2. Then finally open the SLN file (within notepad or any editor) and copy and replace (Ctrl+h) any old name to the new name.

  3. Open VS and click BUILD -> Clean Solution

  4. click Build -> Build solution and then F5 to run...

  5. Note1: If you get something like this: Compilation Error CS0246: The type or namespace name 'project2' could not be found (are you missing a using directive or an assembly reference?)

Source File: c:\Users\Username\AppData\Local\Temp\Temporary ASP.NET Files\root\78dd917f\d0836ce4\App_Web_index.cshtml.a8d08dba.b0mwjmih.0.cs

Then go to the "Temporary ASP.NET Files" folder and delete everything.

  1. Note2: If you are trying to do "save as" to a new named project and to keep also the old one, consider duplicating your db by modifying the connectionStrings in web.config and also by re-starting migrations if you have one in the project.
查看更多
霸刀☆藐视天下
3楼-- · 2019-01-03 11:45

Using the "Find in Files" function of Notepad++ worked fine for me (ctrl + H, Find in Files).

http://notepad-plus-plus.org/download/v6.2.2.html

查看更多
放我归山
4楼-- · 2019-01-03 11:48

If you are Creating a Website in Visual Studio 2010. You can change the project name as follows.

Step 1: In Visual Studio 2010 the SLN file will be stored under project folder within Visual studio 2010 and Source files are stored under Website folder within Visual Studio 2010.

Step 2: Rename the folder by right click on that folder forward by Rename which contains your SLN project.

Step 3: Rename the SLN file name by right click on that SLN file forward by Rename.

Step 4: Rename the folder that contains Source of that SLN file under Website in Visual Studio 2010.

Step 5: Then finally Double click Your SLN file and change the root of your SLN source folder.

查看更多
三岁会撩人
5楼-- · 2019-01-03 11:52

The Rename operations in visual studio only change the filename, i.e. for a project, *.prj and for a solution *.sln. You will need to rename folders seperately using the filesystem, and you will need to remove and readd the projects since they will have new foldernames. However, note that the solution and project files are respectively texst and xml files. You could write your own program that parses them and renames both the folder names, filenames, and fixes the project/solution files internally.

查看更多
Bombasti
6楼-- · 2019-01-03 11:54

If you have problems with loading Shared projects, like Xamarin, remember to change reference to shared libs in csproj files. I developed a CocosSharp game and Droid/iOS/WP81 projects didn't want to load. I had to change below line in every csproj file(Driod/iOS/WP81) which referenced Shared lib. That was caused because of folder names change, so replace YOUR_PREVIOUS_NAMESPACE with your new names of folders.

<Import Project="..\YOUR_PREVIOUS_NAMESPACE.Shared\EmptyProject.Shared.projitems" Label="Shared" />

Also I noticed that for .Driod projects, assembly name in project properties cannot be changed using visual studio(I use 2015). I had to change assembly name manually in the .Droid.csproj file.

<AssemblyName>YourNameSpace</AssemblyName>

Then I loaded solution and in project properties view new name appeared. After rebuilding dll with that name was generated.

查看更多
何必那么认真
7楼-- · 2019-01-03 11:56

Remove/add project file method

This method is entirely aimed at renaming the directory for the project, as viewed in Windows Explorer.

  1. Backup your entire project using something like GIT, SVN or WinZip (important!).
  2. Within the solution in Visual Studio, remove the project.
  3. Rename the directory in Windows Explorer.
  4. Add the project back in again within Visual Studio.

Advantages

  • You can make the directory within Windows Explorer match the project name within the solution.

Disadvantages

  • If you remove a library, it removes any references to said library from other projects. The solution may not compile after this, until you add the references to said library back in (this is quite easy). This is the reason why step 1 (backup) is so important.
  • If you have source control, you will lose the history of the file.

Right clicking on a project and selecting "Open Folder in Windows Explorer" is useful to keep track of where the project is stored while you are performing this process.

查看更多
登录 后发表回答