visual-studio 在 Visual Studio 中重命名解决方案(和目录)的正确方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2043618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 11:23:23  来源:igfitidea点击:

Proper way to rename solution (and directories) in Visual Studio

visual-studio

提问by David Pfeffer

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

我有一个相当复杂的 Visual Studio 解决方案(2010,但应该无关紧要),我需要重命名。

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?

有没有官方方法可以做到这一点?或者(不太好),是否有免费工具来实现这一点?

采纳答案by Contango

Manually edit .sln file

手动编辑 .sln 文件

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

此方法完全旨在重命名项目的目录,如在 Windows 资源管理器中所见。

This method does not suffer from the problems in the Remove/add project file methodbelow (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.

此方法不会遇到下面删除/添加项目文件方法中的问题(引用消失),但如果您的项目处于源代码控制之下,则可能会导致问题(请参阅下面的注释)。这就是第 2 步(备份)如此重要的原因。

  1. Close Visual Studio.
  2. Create a backup of your .sln file (you can always roll back).
  3. Imagine you want to rename directory Project1to Project2.
  4. If not using source control, rename the folder from Project1to Project2using Windows Explorer.
  5. If using source control, rename the folder from Project1to Project2using 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 Project1to 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.
  1. 关闭 Visual Studio。
  2. 创建 .sln 文件的备份(您可以随时回滚)。
  3. 假设您想将目录重命名Project1Project2.
  4. 如果不使用源代码控制,从重命名文件夹Project1,以Project2使用Windows资源管理器。
  5. 如果使用源控制,从重命名文件夹Project1,以Project2使用由源控制供给的功能。这将保留文件的历史记录。例如,使用TortoiseSVN,右键单击文件,选择TortoiseSVN .. Rename
  6. 在 .sln 文件Project1Project2,使用NotePad等文本编辑器编辑to be 的所有实例。
  7. 重新启动 Visual Studio,一切都会像以前一样工作,但项目位于不同的目录中。

You can also see renaming solution manuallyor 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 notremove any references from other projects to this file (an advantage over the Remove/add project filemethod, see my other answer below).
  • 您可以使 Windows 资源管理器中的目录与解决方案中的项目名称匹配。
  • 此方法不会删除其他项目的任何引用此文件(通过一个优势删除/添加项目文件的方法,请参见下面我对方的回答)。

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).
  • 在重命名任何内容之前将所有内容备份到 .zip 文件中很重要,因为这种方法可能会导致源代码控制问题。
  • 如果您的项目受源代码控制,则在源代码控制之外重命名文件或目录(使用 Windows 资源管理器)可能会产生问题。如果可以,最好使用源代码控制框架本身重命名文件,以保留该文件的历史记录(右键单击查看上下文菜单 - 它可能具有重命名文件的功能)。

Update 2014-11-02

更新 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:

ReSharper 添加了一种自动化方法来实现与上述手动方法相同的结果。如果命名空间带有蓝色波浪线下划线,请单击操作金字塔图标以执行以下任一操作:

  • Rename the namespace to match the directory name in Windows Explorer, or;
  • Rename the directory in Windows Explorer to match the namespace.
  • 重命名命名空间以匹配 Windows 资源管理器中的目录名称,或者;
  • 重命名 Windows 资源管理器中的目录以匹配命名空间。

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.

在第二种情况下,最后一个词定义了 Windows 资源管理器中的新目录名称,例如,如果我们将命名空间更改为ViewModel2,它将提供将文件移动到文件夹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

更新 2018-01-31

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

使用 Visual Studio 2008、2010、2012、2013、2015、2017 更新 1、2、3、4、5 进行测试。

Update 2020-05-02

更新 2020-05-02

Tested with Visual Studio 2019.

使用 Visual Studio 2019 进行测试。

回答by Zaheer Ahmed

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.

  1. 在解决方案资源管理器中,右键单击项目,选择重命名,然后输入新名称。
  2. 在解决方案资源管理器中,右键单击项目并选择属性。在应用程序选项卡上,更改“程序集名称”和“默认命名空间”。
  3. 在主 cs 文件(或任何其他代码文件)中,重命名命名空间声明以使用新名称。为此,右键单击命名空间并选择 Refactor > Rename 输入新名称。例如: namespace WindowsFormsApplication1
  4. 更改 Properties/AssemblyInfo.cs 中的 AssemblyTitle 和 AssemblyProduct。

    [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. 物理删除 bin 和 obj 目录。

  6. 重命名项目物理文件夹目录。

  7. 打开 SLN 文件(在记事本或任何编辑器中)并更改项目路径。

  8. 清理并重建项目。

回答by AaronLS

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.

Visual Studio 中的重命名操作仅更改文件名,即对于项目 *.prj 和对于解决方案 *.sln。您需要使用文件系统单独重命名文件夹,并且您需要删除并读取项目,因为它们将具有新的文件夹名称。但是请注意,解决方案和项目文件分别是 texst 和 xml 文件。您可以编写自己的程序来解析它们并重命名文件夹名称、文件名,并在内部修复项目/解决方案文件。

回答by edoardo

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

您还可以导出模板,然后从导出的模板创建一个新项目,根据需要更改名称

回答by Abhishek Agrawal

Below is a step-by-step way of renaming your entire solution in Visual Studio 2013

下面是在 Visual Studio 2013 中重命名整个解决方案的分步方法

Assuming we're changing a project named "HelloWorld.ui" to "Section1to5.ui"

假设我们将一个名为“HelloWorld.ui”的项目更改为“Section1to5.ui”

  1. Go to solution explorer (screenshot below), right click on the project that you wish to rename and select "Rename". Enter the new name that you desire.
  1. 转到解决方案资源管理器(下面的屏幕截图),右键单击要重命名的项目并选择“重命名”。输入您想要的新名称。

Solution Explorer Screenshot

解决方案资源管理器截图

  1. Right click on the Project that you have just renamed, and click on "Properties". Under the Application tab, change the "Assembly name" and "Default namespace".

  2. In the main Program.cs file (or any other code files that you may have created), 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 HelloWorld.ui ----> namespace Section1to5.ui

  3. Next, expand the "Properties" and double click on AssemblyInfo.cs file. You will notice the below code fragment:

  1. 右键单击刚刚重命名的项目,然后单击“属性”。在应用程序选项卡下,更改“程序集名称”和“默认命名空间”。

  2. 在主 Program.cs 文件(或您可能创建的任何其他代码文件)中,重命名命名空间声明以使用新名称。为此,右键单击命名空间并选择 Refactor > Rename 输入新名称。例如:

    命名空间 HelloWorld.ui ----> 命名空间 Section1to5.ui

  3. 接下来,展开“属性”并双击 AssemblyInfo.cs 文件。您会注意到以下代码片段:

[assembly: AssemblyTitle("HelloWorld.ui")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Microsoft")]
[assembly: AssemblyProduct("HelloWorld.ui")]
[assembly: AssemblyCopyright("Copyright ? Microsoft 2015")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: AssemblyTitle("HelloWorld.ui")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Microsoft")]
[assembly: AssemblyProduct("HelloWorld.ui")]
[assembly: AssemblyCopyright("Copyright ? Microsoft 2015")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

You need to change the AssemblyTitle and AssemblyProduct in Properties/AssemblyInfo.cs to the new name, in this example from "HelloWorld.ui" to "Section1to5.ui". Once you're done changing the name, save and close Visual Studio.

您需要将 Properties/AssemblyInfo.cs 中的 AssemblyTitle 和 AssemblyProduct 更改为新名称,在此示例中从“HelloWorld.ui”更改为“Section1to5.ui”。完成更改名称后,保存并关闭 Visual Studio。

  1. In the project directory, go inside the folder "HelloWorld.ui" and delete the "bin" and "obj" directories.

  2. Once you're done, rename the folder "HelloWorld.ui" to the new project name.

  3. Open the renamed solutions file with a text editor such as Notepad or Notepad++ and edit the following line:

    Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HelloWorld.ui", "HelloWorld.ui\HelloWorld.ui.csproj", "{39FC65A3-3AE7-4EC9-B8F7-74F971636C70}"

  1. 在项目目录中,进入文件夹“HelloWorld.ui”并删除“bin”和“obj”目录。

  2. 完成后,将文件夹“HelloWorld.ui”重命名为新项目名称。

  3. 使用 Notepad 或 Notepad++ 等文本编辑器打开重命名的解决方案文件并编辑以下行:

    项目("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HelloWorld.ui", "HelloWorld.ui\HelloWorld.ui.csproj", "{39FC65A3-3AE7-4EC9-B8F7-74F971636C7

Replace all the instances of "HelloWorld.ui" the new name that you've selected for your project, in this case "Section1to5.ui". This changes the path of the project to the renamed directory in step 6.

将“HelloWorld.ui”的所有实例替换为您为项目选择的新名称,在本例中为“Section1to5.ui”。这会将项目的路径更改为步骤 6 中重命名的目录。

  1. Open the solutions file with visual studio, and clean and rebuild the project.

  2. You may notice that the "Debug/Run" button has been replaced by the "Attach" button. If this happens, simply right click on the project and choose the "Set as StartUp project" option. Alternatively, click on Tools>Customize>Commands>Add command>Debug and add the button.

  1. 使用 Visual Studio 打开解决方案文件,并清理并重建项目。

  2. 您可能会注意到“调试/运行”按钮已被“附加”按钮取代。如果发生这种情况,只需右键单击该项目并选择“设置为启动项目”选项。或者,单击工具>自定义>命令>添加命令>调试并添加按钮。

You're done renaming the project, if you have any doubts feel free to post and I'd be glad to help out!

您已完成对项目的重命名,如果您有任何疑问,请随时发布,我很乐意提供帮助!

回答by Michael

Delete your bin and obj subfolders to remove a load of incorrect reference then use windows to search for old name.

删除 bin 和 obj 子文件夹以删除大量不正确的引用,然后使用 Windows 搜索旧名称。

Edit any code or xml files found and rebuild, should be ok now.

编辑找到的任何代码或 xml 文件并重建,现在应该可以了。

回答by ToastedSoul

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

我已经使用 Visual Studio 扩展“完全重命名项目”在 ASP.NET Core 2 解决方案中成功重命名项目。

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

然后我使用 ReSharper 来调整命名空间(右键单击项目,重构,调整命名空间...)

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

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

enter image description here

在此处输入图片说明

回答by Cameron

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

我尝试了 Visual Studio Project Renamer,但是在我尝试的少数情况下它抛出了异常。

I have had good success with CopyWiz.

我在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.

我与他们没有任何关联,而且我还没有为此付费,但我可能很快就要付费了,因为它是似乎对我的 C# 和 C++ 项目都有效的唯一工具。不过,我确实希望他们能赚一点钱,并继续改进。

回答by Contango

Remove/add project file method

删除/添加项目文件方法

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

此方法完全旨在重命名项目的目录,如在 Windows 资源管理器中所见。

  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.
  1. 使用 GIT、SVN 或 WinZip 之类的东西备份整个项目(很重要!)。
  2. 在 Visual Studio 的解决方案中,删除项目。
  3. 在 Windows 资源管理器中重命名目录。
  4. 在 Visual Studio 中重新添加该项目。

Advantages

好处

  • You can make the directory within Windows Explorer match the project name within the solution.
  • 您可以使 Windows 资源管理器中的目录与解决方案中的项目名称匹配。

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.
  • 如果您删除一个库,它会从其他项目中删除对该库的任何引用。在此之后,解决方案可能无法编译,直到您重新添加对所述库的引用(这很容易)。这就是第 1 步(备份)如此重要的原因。
  • 如果您有源代码控制,您将丢失文件的历史记录。

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.

右键单击项目并选择“在 Windows 资源管理器中打开文件夹”对于在执行此过程时跟踪项目的存储位置很有用。

回答by Wotuu

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

使用 Notepad++ 的“在文件中查找”功能对我来说效果很好(ctrl + H,在文件中查找)。

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

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