在 Visual Studio 中重命名解决方案(和目录)的正确方法 [英] Proper way to rename solution (and directories) in Visual Studio

查看:35
本文介绍了在 Visual Studio 中重命名解决方案(和目录)的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

手动编辑 .sln 文件

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

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

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. 关闭 Visual Studio.
  2. 创建 .sln 文件的备份(您可以随时回滚).
  3. 假设您要将目录 Project1 重命名为 Project2.
  4. 如果不使用源代码管理,请使用 Windows 资源管理器将文件夹从 Project1 重命名为 Project2.
  5. 如果使用源代码管理,请使用源代码管理提供的函数将文件夹从 Project1 重命名为 Project2.这将保留文件的历史记录.例如,使用 TortoiseSVN,右键单击该文件,选择 TortoiseSVN .. Rename.
  6. 在 .sln 文件中,使用诸如 NotePad.
  7. 重新启动 Visual Studio,一切都会像以前一样工作,但项目位于不同的目录中.
  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.

您还可以查看手动重命名解决方案描述此手动过程的帖子.

优势

  • 您可以使 Windows 资源管理器中的目录与解决方案中的项目名称匹配.
  • 此方法不会删除其他项目对此文件的任何引用(优于删除/添加项目文件方法,请参阅下面我的其他答案).
  • 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).

警告

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

更新 2014-11-02

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

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:

  • 重命名命名空间以匹配 Windows 资源管理器中的目录名称,或者;
  • 在 Windows 资源管理器中重命名目录以匹配命名空间.

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

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.

更新 2018-01-31

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

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

更新 2020-05-02

使用 Visual Studio 2019 测试.

Tested with Visual Studio 2019.

更新 2021-11-19

重新测试.仍然有效.

这篇关于在 Visual Studio 中重命名解决方案(和目录)的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆