我有一个相当复杂的Visual Studio解决方案(2010年,但这不重要),我需要重命名。
我想重命名文件夹以匹配新的解决方案名称,但我无法找到自动重构文件夹名称的方法,而且查看每个项目文件将是一件痛苦的事情。
有官方的做法吗?
我有一个相当复杂的Visual Studio解决方案(2010年,但这不重要),我需要重命名。
我想重命名文件夹以匹配新的解决方案名称,但我无法找到自动重构文件夹名称的方法,而且查看每个项目文件将是一件痛苦的事情。
有官方的做法吗?
当前回答
手动编辑。sln文件
这个方法完全是为了重命名项目的目录,就像在Windows资源管理器中看到的那样。
这种方法不会遇到下面的删除/添加项目文件方法中的问题(引用消失),但如果项目处于源代码控制之下,它可能会导致问题(参见下面的注释)。这就是为什么第二步(备份)如此重要。
Close Visual Studio. Create a backup of your .sln file (you can always roll back). Imagine you want to rename directory Project1 to Project2. If not using source control, rename the folder from Project1 to Project2 using Windows Explorer. 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. In the .sln file, edit all instances of Project1 to be Project2, using a text editor like NotePad. Restart Visual Studio, and everything will work as before, but with the project in a different directory.
你也可以看到重命名解决方案手动或帖子,其中描述了这个手动过程。
优势
您可以使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添加了一个自动化的方法来实现与上面的手动方法相同的结果。如果命名空间下划线是弯曲的蓝色线,单击动作金字塔图标:
重命名命名空间以匹配Windows资源管理器中的目录名称,或者; 重命名Windows资源管理器中的目录以匹配命名空间。
在第二种情况下,最后一个词定义了Windows资源管理器中的新目录名称,例如,如果我们将命名空间更改为ViewModel2,它将提供将文件移动到文件夹ViewModel2。
但是,这并不一定会更新源代码控制中的文件,因此您可能仍然必须使用手动方法。
更新2018-01-31
用Visual Studio 2008、2010、2012、2013、2015、2017更新1、2、3、4、5测试。
更新2020-05-02
使用Visual Studio 2019进行测试。
更新2021-11-19
重新测试。仍能工作。
更新2022-11-30
最新版本的Git应该自动检测文件重命名,因此不需要额外注意保存差异的历史。
其他回答
重命名一个解决方案:
In Solution Explorer, right-click the project, select Rename, and enter a new name. In Solution Explorer, right-click the project and select Properties. On the Application tab, change the "Assembly name" and "Default namespace". 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 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("")] Delete bin and obj directories physically. Rename the project physical folder directory. Open the SLN file (within notepad or any editor) and change the path to the project. Clean and Rebuild the project.
删除/添加项目文件方法
这个方法完全是为了重命名项目的目录,就像在Windows资源管理器中看到的那样。
使用GIT、SVN或WinZip(很重要!)等工具备份整个项目。 在Visual Studio中的解决方案中,删除项目。 在Windows资源管理器中重命名目录。 在Visual Studio中再次添加项目。
优势
您可以使Windows资源管理器中的目录与解决方案中的项目名称相匹配。
缺点
如果删除一个库,它将从其他项目中删除对该库的任何引用。在此之后,解决方案可能无法编译,直到您将该库的引用添加回(这非常简单)。这就是第一步(备份)如此重要的原因。 如果您有源代码控制,则会丢失文件的历史记录。
右键单击一个项目并选择“在Windows资源管理器中打开文件夹”对于跟踪项目在执行此过程中的存储位置非常有用。
我遵循https://gist.github.com/n3dst4/b932117f3453cc6c56be链接,我能够重命名我的整个解决方案成功。
删除bin和obj子文件夹以删除不正确的引用,然后使用windows搜索旧名称。
编辑任何代码或xml文件发现和重建,现在应该是好的。
如果在加载Xamarin等共享项目时遇到问题,请记住在csproj文件中更改对共享库的引用。我开发了一款CocosSharp游戏,但Droid/iOS/WP81项目不想加载。我不得不在每个csproj文件(Driod/iOS/WP81)中更改下面的行,其中引用了共享库。这是由于文件夹名称更改造成的,因此将YOUR_PREVIOUS_NAMESPACE替换为文件夹的新名称。
<Import Project="..\YOUR_PREVIOUS_NAMESPACE.Shared\EmptyProject.Shared.projitems" Label="Shared" />
另外,我注意到对于. driod项目,项目属性中的程序集名称不能使用Visual Studio更改(我使用2015年)。我必须在. droid中手动更改程序集名称。csproj文件。
<AssemblyName>YourNameSpace</AssemblyName>
然后我加载解决方案,在项目属性视图新名称出现。重建dll后生成该名称。