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条回答
Root(大扎)
2楼-- · 2019-01-03 12:04

You can also export template and then create a new project from the exported template changing the name as you prefer

查看更多
聊天终结者
3楼-- · 2019-01-03 12:05

Manually edit .sln file

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

This method does not suffer from the problems in the Remove/add project file method below (references disappearing), but it can result in problems if your project is under source control (see notes below). This is why step 2 (backup) is so important.

  1. Close Visual Studio.
  2. Create a backup of your .sln file (you can always roll back).
  3. Imagine you want to rename directory "Project1" to "Project2".
  4. If not using source control, rename the folder from "Project1" to "Project2" using Windows Explorer.
  5. If using source control, rename the folder from "Project1" to "Project2" using the functions supplied by source control. This preserves the history of the file. For example, with TortoiseSVN, right click on the file, select TortoiseSVN .. Rename.
  6. In the .sln file, edit all instances of "Project1" to be "Project2", using a text editor like NotePad.
  7. Restart Visual Studio, and everything will work as before, but with the project in a different directory.

You can also see renaming solution manually or post which describes this manual process.

Advantages

  • You can make the directory within Windows Explorer match the project name within the solution.
  • This method does not remove any references from other projects to this file (an advantage over the Remove/add project file method, see my other answer below).

Warnings

  • It's important to back everything up into a .zip file before renaming anything, as this method can create issues with source control.
  • If your project is under source control, it may create issues if you rename files or directories outside of source control (using Windows Explorer). Its preferable to rename the file using the source control framework itself, if you can, to preserve the history of that file (check out the context menu on a right click - it may have a function to rename the file).

Update 2014-11-02

ReSharper has added an automated method for achieving the same result as the manual method above. If the namespace is underlined with a squiggly blue line, click on the action pyramid icon to either:

  • Rename the namespace to match the directory name in Windows Explorer, or;
  • Rename the directory in Windows Explorer to match the namespace.

In the second case, the final word defines the new directory name in Windows Explorer, e.g. if we changed the namespace to ViewModel2, it would offer to move the file to folder ViewModel2.

However, this will not necessarily update files in source control, so you may still have to use the manual method.

enter image description here

Update 2018-01-31

Tested with Visual Studio 2008, 2010, 2012, 2013, 2015, 2017 Update 1, 2, 3, 4, 5.

查看更多
成全新的幸福
4楼-- · 2019-01-03 12:05

Here are steps you need to follow to rename a solution:

  1. In Solution Explorer, right-click the project, select Rename, and enter a new name.
  2. In Solution Explorer, right-click the project and select Properties. On the Application tab, change the "Assembly name" and "Default namespace".
  3. In the main cs file (or any other code files), 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 WindowsFormsApplication1
  4. Change the AssemblyTitle and AssemblyProduct in Properties/AssemblyInfo.cs.

    [assembly: AssemblyTitle("New Name Here")]
    [assembly: AssemblyDescription("")]
    [assembly: AssemblyConfiguration("")]
    [assembly: AssemblyCompany("")]
    [assembly: AssemblyProduct("New Name Here")]
    [assembly: AssemblyCopyright("Copyright ©  2013")]
    [assembly: AssemblyTrademark("")]
    [assembly: AssemblyCulture("")]
    
  5. Delete bin and obj directories physically.

  6. Rename the project physical folder directory.

  7. Open the SLN file (within notepad or any editor) and change the path to the project.

  8. Clean and Rebuild the project.

查看更多
你好瞎i
5楼-- · 2019-01-03 12:07

I tried the Visual Studio Project Renamer, but, it threw exceptions in the few cases where I tried it.

I have had good success with CopyWiz.

I am in no way affiliated with them, and I have not yet paid for it, but I may have to soon, as it is the only tool that seems to work for both my C# and C++ projects. I do hope they make a little money, and continue to improve it, though.

查看更多
虎瘦雄心在
6楼-- · 2019-01-03 12:10

I've used the Visual Studio extension "Full Rename Project" to successfully rename projects in an ASP.NET Core 2 solution.

I used ReSharper then to adjust the namespace (right click on project, refactor, adjust namespaces...)

https://github.com/kuanysh-nabiyev/RenameProjectVsExtension

enter image description here

查看更多
登录 后发表回答