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

查看:490
本文介绍了在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资源管理器中查看,此方法完全旨在重命名项目目录.

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

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文件中,使用诸如Project1的所有实例都编辑为Project2. rel ="noreferrer">记事本.
  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年1月31日

在Visual Studio 2008、2010、2012、2013、2015、2017 Update 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.

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

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