visual-studio 如何从 Visual Studio 中重命名项目文件夹?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/211241/
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 09:47:18  来源:igfitidea点击:

How can I rename a project folder from within Visual Studio?

visual-studiodirectoryprojects-and-solutions

提问by andersjanmyr

My current solution for renaming the project folder is:

我当前重命名项目文件夹的解决方案是:

  • Remove the project from the solution.
  • Rename the folder outside Visual Studio.
  • Re-add the project to the solution.
  • 从解决方案中删除项目。
  • 重命名 Visual Studio 外的文件夹。
  • 将项目重新添加到解决方案中。

Is there a better way?

有没有更好的办法?

回答by andersjanmyr

TFS users:If you are using source control that requires you to warn it before your rename files/folders then look at this answer insteadwhich covers the extra steps required.

TFS 用户:如果您使用的源代码管理要求您在重命名文件/文件夹之前发出警告,请查看此答案,其中包含所需的额外步骤。



To rename a project's folder, file(.*proj) and display namein Visual Studio:

要在 Visual Studio 中重命名项目的文件夹文件( .*proj) 和显示名称

  • Close the solution.
  • Rename the folder(s) outside Visual Studio. (Rename in TFS if using source control)
  • Open the solution, ignoring the warnings (answer "no" if asked to load a project from source control).
  • Go through all the unavailableprojects and...
    • Open the propertieswindow for the project (highlight the project and press Alt+Enteror F4, or right-click > properties).
    • Set the property 'File Path' to the new location.
      • If the property is not editable (as in Visual Studio 2012), then open the .slnfile directly in another editor such as Notepad++and update the paths there instead. (You may need to check-out the solution first in TFS, etc.)
    • Reloadthe project - right-click > reload project.
    • Change the display name of the project, by highlighting it and pressing F2, or right-click > rename.
  • 关闭解决方案。
  • 重命名 Visual Studio 外的文件夹。(如果使用源代码管理,请在 TFS 中重命名)
  • 打开解决方案,忽略警告(如果要求从源代码管理加载项目,请回答“否”)。
  • 浏览所有不可用的项目并...
    • 打开项目的属性窗口(突出显示项目并按Alt+EnterF4,或右键单击 > 属性)。
    • 将属性“文件路径”设置为新位置。
      • 如果该属性不可编辑(如在 Visual Studio 2012 中),则.sln直接在另一个编辑器(如Notepad++)中打开该文件并在那里更新路径。(您可能需要先在 TFS 等中签出解决方案)
    • 重新加载项目 - 右键单击​​ > 重新加载项目。
    • 更改项目的显示名称,方法是突出显示并按F2,或右键单击 >重命名

Note:Other suggested solutions that involve removing and then re-adding the project to the solution will break project references.

注意:涉及删除项目然后将项目重新添加到解决方案的其他建议解决方案将破坏项目引用。

If you perform these steps then you might also consider renaming the following to match:

如果您执行这些步骤,那么您还可以考虑重命名以下内容以匹配:

  1. Assembly
  2. Default/Root Namespace
  3. Namespace of existing files (use the refactor tools in Visual Studio or ReSharper'sinconsistent namespaces tool)
  1. 部件
  2. 默认/根命名空间
  3. 现有文件的命名空间(使用 Visual Studio 中的重构工具或ReSharper 的不一致命名空间工具)

Also consider modifying the values of the following assembly attributes:

还要考虑修改以下程序集属性的值:

  1. AssemblyProductAttribute
  2. AssemblyDescriptionAttribute
  3. AssemblyTitleAttribute
  1. AssemblyProductAttribute
  2. AssemblyDescriptionAttribute
  3. AssemblyTitleAttribute

回答by rabashani

There is another way doing this, using the *.sol, *csproj files.

还有另一种方法,使用 *.sol, *csproj 文件。

  1. Open your solution file.
  2. Search for the *.csproj you would like to change.
  3. It will be like this (relative to the *.sol file):

    Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Shani.Commands.Impl", "Shani.Commands.Impl\Shani.Commands.Impl.csproj", "{747CFA4B-FC83-419A-858E-5E2DE2B948EE}"

  4. And just change the first part to the new diretory for example:

    Impl\Shani.Commands.Impl\Shani.Commands.Impl.csproj

  5. Of course, don't forget to move the whole project to that directory.

  1. 打开您的解决方案文件。
  2. 搜索您要更改的 *.csproj。
  3. 它会是这样的(相对于 *.sol 文件):

    Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Shani.Commands.Impl", "Shani.Commands.Impl\Shani.Commands.Impl.csproj", "{747CFA4B-FC83-419A-858E-5E2DE2B948EE}"

  4. 只需将第一部分更改为新目录,例如:

    Impl\Shani.Commands.Impl\Shani.Commands.Impl.csproj

  5. 当然,不要忘记将整个项目移动到该目录中。

回答by wired00

This is straightforward in Visual Studio 2015(possibly works in older versions)

这在 Visual Studio 2015 中很简单(可能适用于旧版本)

  1. In Solution Explorer, right click on Main solutionRename
  2. In Solution Explorer, right click on project(under solution) → Rename
  3. In Solution Explorer, double click, or right click on Properties→ goto Application Tab, rename Assembly nameand Default namespaceto match.
  4. If you wish to also rename the namespace, open one of your class files. Right click the namespace → Rename.... This should search and replace all references to your namespace throughout the project.
  5. Close the project → rename the project folder.
  6. Edit the .slnfile in Notepad, and change the path to the csproj, i.e., fu\bar.csprojbar\bar.csproj.
  1. 在解决方案资源管理器中,右键单击主解决方案重命名
  2. 在解决方案资源管理器中,右键单击项目(在解决方案下)→重命名
  3. 在解决方案资源管理器中,双击或右键单击Properties→ goto Application Tab,重命名程序集名称默认命名空间以匹配。
  4. 如果您还想重命名命名空间,请打开您的类文件之一。右键单击命名空间 → Rename...。这应该在整个项目中搜索并替换对您的命名空间的所有引用。
  5. 关闭项目 → 重命名项目文件夹
  6. .sln记事本中编辑该文件,并更改 csproj 的路径,即fu\bar.csprojbar\bar.csproj

回答by René

Man, have I struggled with this. Unfortunately there isn't a one click solution in Visual Studio, but if you're running Visual Studio 2012and your project is under source control with Team Foundation Server, here is how I got it to work, while keeping the source history:

伙计,我有没有为此挣扎过。不幸的是,Visual Studio 中没有一键式解决方案,但是如果您运行的是Visual Studio 2012并且您的项目受Team Foundation Server 的源代码控制,那么我是如何让它工作的,同时保留源历史记录:

(Make sure you read @mjv's comment below, as he notes that you can skip step 5-10)

(请务必阅读下面@mjv 的评论,因为他指出您可以跳过步骤 5-10)

  1. Make sure you have checked in all changes, so you have no pending changes.
  2. Remove the project from the solution, by right clicking and selecting Remove.
  3. Now, in Windows Explorer, rename the project folder.
  4. Go back to Visual Studio, and in Solution Explorer, right click the solution and choose Add-> Existing project. Select the project file for the project you removed in step 2, which should be located in the renamed folder.
  5. Now the project is back in the solution, but the project doesn't seem to be added to source control. To fix that, open Source Control Explorer.
  6. Find the project folder in Source Control Explorer, that corresponds with the project folder on your disk, that you renamed in step 3.
  7. Rename the folder in Source Control Explorer, so it has the same name as the project folder on disk.
  8. Now take a look at your pending changes. You should have changes to the solution file and a rename operation on the project folder.
  9. Do a rebuild and make sure everything compiles correctly. If you had inter-project references to the project you renamed, you need to add them again to the individual projects that referenced it.
  10. You should be all set now. Go and check everything in.
  1. 确保您已签入所有更改,因此您没有挂起的更改。
  2. 通过右键单击并选择Remove从解决方案中删除项目。
  3. 现在,在 Windows 资源管理器中,重命名项目文件夹。
  4. 返回 Visual Studio,在解决方案资源管理器中,右键单击解决方案并选择Add-> Existing project。为您在步骤 2 中删除的项目选择项目文件,该文件应位于重命名的文件夹中。
  5. 现在该项目又回到了解决方案中,但该项目似乎没有被添加到源代码管理中。要解决此问题,请打开源代码管理资源管理器。
  6. 在源代码管理资源管理器中找到项目文件夹,该文件夹与您在步骤 3 中重命名的磁盘上的项目文件夹相对应。
  7. 在源代码管理资源管理器中重命名文件夹,使其与磁盘上的项目文件夹具有相同的名称。
  8. 现在看看您的待定更改。您应该对解决方案文件进行更改并对项目文件夹进行重命名操作。
  9. 进行重建并确保一切都正确编译。如果您对重命名的项目有项目间引用,则需要将它们再次添加到引用它的各个项目中。
  10. 你现在应该已经准备好了。去检查一切。

The above guide worked for me. If it doesn't work for you, try and delete your local solution completely, and remove the folder mapping in your workspace. Restart Visual Studio just in case. Make sure you actually deleted the whole solution from your computer. Now readd the solution mapping to your workspace and get the latest version. Now try the above steps. The same applies if something goes wrong while following the above steps. Just delete your solution locally and get the latest source, and you'll have a clean slate to work with.

上述指南对我有用。如果它对您不起作用,请尝试完全删除本地解决方案,并删除工作区中的文件夹映射。重新启动 Visual Studio 以防万一。确保您确实从计算机中删除了整个解决方案。现在阅读映射到您的工作区的解决方案并获取最新版本。现在尝试上述步骤。如果在执行上述步骤时出现问题,这同样适用。只需在本地删除您的解决方案并获取最新的源代码,您就会有一个全新的工作环境。

If you're still having problems, make sure that you haven't changed anything manually in the solution file, or trying other 'tricks' before trying the above steps. If you have changed something and checked it in, you might want to consider doing a rollback to the point just before you started messing with the renaming of the project.

如果您仍然遇到问题,请确保您没有在解决方案文件中手动更改任何内容,或者在尝试上述步骤之前尝试其他“技巧”。如果您更改了某些内容并将其签入,您可能需要考虑回滚到您开始搞乱项目重命名之前的那个点。

Of course, you'd also want to rename the project itself, in Solution Explorer. You can do this before the steps above, but in that case, make sure you check in that change before applying the steps above. You can also do it afterwards, but make sure you follow all the steps above first, and check in your changes before trying to rename the project name in Solution Explorer. I don't recommend trying to mix the above steps with a rename of the project name in Solution Explorer. It might work though, but I would recommand doing it in 2 separate changesets.

当然,您还想在解决方案资源管理器中重命名项目本身。您可以在上述步骤之前执行此操作,但在这种情况下,请确保在应用上述步骤之前签入该更改。您也可以在之后执行此操作,但请确保先执行上述所有步骤,并在尝试在解决方案资源管理器中重命名项目名称之前签入您的更改。我不建议尝试将上述步骤与解决方案资源管理器中项目名称的重命名混合使用。虽然它可能有效,但我建议在 2 个单独的变更集中进行。

回答by mortenbpost

Currently, no. Well, actually you can click the broken project node and in the properties pane look for the property 'Path', click the small browse icon, and select the new path.

目前,没有。好吧,实际上您可以单击损坏的项目节点,然后在属性窗格中查找属性“路径”,单击浏览小图标,然后选择新路径。

Voilà :)

瞧:)

回答by Md. Shafiqur Rahman

The simpler solution is the following:

更简单的解决方案如下:

  1. Right-click the project and rename it.
  2. (optional) Open the project's property settings and modify the assembly name (and optionally the default namespace) to use the new project name.
  3. (optional) Select the namespace name in a source file, right click and select Refactor/Renameto globally rename the namespace to the new project name.
  4. (optional) Open the AssemblyInfo.cs file and change the assembly name to match.
  5. Save and close the solution.
  6. Using Windows Explorer, rename the project folder to the new name.
  7. Open the SLN file in a text editor and find the one reference to the project path and change it to use the new folder name.
  1. 右键单击项目并重命名。
  2. (可选)打开项目的属性设置并修改程序集名称(以及可选的默认命名空间)以使用新的项目名称。
  3. (可选)在源文件中选择命名空间名称,右键单击并选择重构/重命名以将命名空间全局重命名为新项目名称。
  4. (可选)打开 AssemblyInfo.cs 文件并更改程序集名称以匹配。
  5. 保存并关闭解决方案。
  6. 使用 Windows 资源管理器,将项目文件夹重命名为新名称。
  7. 在文本编辑器中打开 SLN 文件,找到对项目路径的一个引用,并将其更改为使用新文件夹名称。

There are four needed steps, but seven recommended. At the end of the day though the project is renamed completely. Technically, the folder name for the project doesn't have to match the project itself, so even that step is optional, but it can be confusing if they don't match. The same for the assembly and namespace names.

有四个必需的步骤,但建议使用七个。在一天结束时,尽管该项目已完全重命名。从技术上讲,项目的文件夹名称不必与项目本身匹配,因此即使该步骤也是可选的,但如果它们不匹配,则可能会造成混淆。程序集和命名空间名称相同。

回答by CRice

In andersjanmyr's answerit's easier to rename the project first.

andersjanmyr 的回答中,首先重命名项目更容易。

  1. Rename the project.
  2. Close the solution (save it).
  3. Rename the folders outside Visual Studio.
  4. Open the solution, ignoring the warnings.
  5. Go through all unavailable projects and set the property 'File Path' to the new location of your project file, i.e. someproject.csproj.
  6. Reload the project.
  1. 重命名项目。
  2. 关闭解决方案(保存)。
  3. 重命名 Visual Studio 外的文件夹。
  4. 打开解决方案,忽略警告。
  5. 浏览所有不可用的项目并将属性“文件路径”设置为项目文件的新位置,即someproject.csproj
  6. 重新加载项目。

Also, after those steps are carried out, you might want to rename other references to your old project name.

此外,在执行这些步骤后,您可能希望重命名对旧项目名称的其他引用。

In project properties, update the Assembly Name and Default Namespace.

在项目属性中,更新程序集名称和默认命名空间。

This will update the following in the project file...

这将更新项目文件中的以下内容...

<RootNamespace>SomeProjectName</RootNamespace>
<AssemblyName>SomeProjectName</AssemblyName>

...and will get rid of the error "Namespace does not correspond to file location, should be: 'SomeProjectName'"

...并且将摆脱错误“命名空间与文件位置不对应,应该是:'SomeProjectName'”

Rename your root namespace (if you have ReSharper right click the Namespace and go Refactor -> Rename).

重命名您的根命名空间(如果您有 ReSharper,请右键单击命名空间并进行重构 -> 重命名)。

Change all occurrences of your old project name in AssemblyInfo.cs.

AssemblyInfo.cs 中更改所有出现的旧项目名称。

回答by Kuanysh

For Visual Studio 2017 you can use my Visual Studio extension:

对于 Visual Studio 2017,您可以使用我的 Visual Studio 扩展:

Download

下载

It will rename the project in:

它会将项目重命名为:

  • The project location folder
  • The solution file
  • References in other projects
  • The assembly name, information
  • The default namespace
  • 项目位置文件夹
  • 解决方案文件
  • 其他项目中的参考
  • 程序集名称、信息
  • 默认命名空间

回答by John Im

I just had to do this myself (using Visual Studio 2010). As some folks have answered, the simplest step seems to be:

我只需要自己做(使用 Visual Studio 2010)。正如一些人所回答的那样,最简单的步骤似乎是:

  1. Close the Visual Studio project.
  2. Open the .sln file and rename the project directory prefix for each of the .csproj occurrences.
  3. Save the .sln file
  4. Rename the actual project folder directory on your hard drive to match your changes in the .sln file.
  5. Open the .sln (in Visual Studio) and rebuild
  1. 关闭 Visual Studio 项目。
  2. 打开 .sln 文件并为每个 .csproj 事件重命名项目目录前缀。
  3. 保存 .sln 文件
  4. 重命名硬盘驱动器上的实际项目文件夹目录以匹配 .sln 文件中的更改。
  5. 打开 .sln(在 Visual Studio 中)并重建

回答by ivke

For those using Visual Studio + Git and wanting to keep the file history (works renaming both projects and/or solutions):

对于那些使用 Visual Studio + Git 并希望保留文件历史记录的人(重命名项目和/或解决方案):

  1. Close Visual Studio

  2. In the .gitignore file, duplicate all ignore paths of the project you want to rename with renamed versions of those paths.

  3. Use the Git move command like this:

    git mv <old_folder_name> <new_folder_name>
    

    See documentation for additional options: https://git-scm.com/docs/git-mv

  4. In your .sln file: Find the line defining your project and change the folder name in path. The line should look something like:

    Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "<Project name>", "<path-to-project>\<project>.csproj"
    
  5. Open Visual Studio, and right click on project → Rename

  6. Afterwards, rename the namespaces.

    I read that ReSharperhas some options for this. But simple find/replace did the job for me.

  7. Remove old .gitignore paths.

  1. 关闭 Visual Studio

  2. 在 .gitignore 文件中,使用这些路径的重命名版本复制要重命名的项目的所有忽略路径。

  3. 像这样使用 Git move 命令:

    git mv <old_folder_name> <new_folder_name>
    

    有关其他选项,请参阅文档:https: //git-scm.com/docs/git-mv

  4. 在您的 .sln 文件中:找到定义您的项目的行并更改路径中的文件夹名称。该行应如下所示:

    Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "<Project name>", "<path-to-project>\<project>.csproj"
    
  5. 打开 Visual Studio,右键单击项目 →重命名

  6. 然后,重命名命名空间。

    我读到ReSharper对此有一些选择。但简单的查找/替换为我完成了这项工作。

  7. 删除旧的 .gitignore 路径。