如何在 Visual Studio 中重命名项目文件夹?

发布于 2024-07-06 16:01:01 字数 142 浏览 3 评论 0 原文

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

  • 从解决方案中删除项目。
  • 在 Visual Studio 外部重命名该文件夹。
  • 将项目重新添加到解决方案中。

有没有更好的办法?

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.

Is there a better way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(30

顾铮苏瑾 2024-07-13 16:01:01

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


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

  • 关闭解决方案。
  • 重命名 Visual Studio 外部的文件夹。 (如果使用源代码管理,则在 TFS 中重命名)
  • 打开解决方案,忽略警告(如果要求从源代码管理加载项目,请回答“否”)。
  • 浏览所有不可用项目并...
    • 打开项目的属性窗口(突出显示该项目并按 Alt+EnterF4,或右键单击>属性)。
    • 将属性“文件路径”设置为新位置。
    • 重新加载项目 - 右键单击​​> 重新加载项目。
    • 通过突出显示项目并按 F2 或右键单击 > 来更改项目的显示名称。 重命名

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

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

  1. 程序集
  2. 默认/根命名空间
  3. 现有文件的命名空间(使用 Visual Studio 中的重构工具或 ReSharper 的 不一致的命名空间工具)

另请考虑修改以下值 程序集属性

  1. AssemblyProductAttribute
  2. AssemblyDescriptionAttribute
  3. AssemblyTitleAttribute

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


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

  • 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 unavailable projects and...
    • Open the properties window for the project (highlight the project and press Alt+Enter or 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 .sln file 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.)
    • Reload the project - right-click > reload project.
    • Change the display name of the project, by highlighting it and pressing F2, or right-click > rename.

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's inconsistent namespaces tool)

Also consider modifying the values of the following assembly attributes:

  1. AssemblyProductAttribute
  2. AssemblyDescriptionAttribute
  3. AssemblyTitleAttribute
多像笑话 2024-07-13 16:01:01

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

  1. 在解决方案资源管理器中,右键单击主解决方案重命名
  2. 在解决方案中资源管理器,右键单击项目(在解决方案下)→ 重命名
  3. 在解决方案资源管理器中,双击或右键单击属性 → 转到 “应用程序”选项卡,重命名程序集名称默认命名空间以匹配。
  4. 如果您还希望重命名命名空间,请打开您的类文件之一。 右键单击命名空间→重命名...。 这应该搜索并替换整个项目中对命名空间的所有引用。
  5. 关闭项目 → 重命名项目文件夹
  6. 记事本中编辑.sln文件,并更改csproj 的路径,即 fu\bar.csprojbar\bar.csproj

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

  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 name and Default namespace to 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 .sln file in Notepad, and change the path to the csproj, i.e., fu\bar.csprojbar\bar.csproj.
旧时模样 2024-07-13 16:01:01

还有另一种方法可以做到这一点,即使用 *.sol、*csproj 文件。

  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. 当然,不要忘记将整个项目移动到该目录。

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

  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.

油饼 2024-07-13 16:01:01

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

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

  1. 确保您已签入所有更改,因此您没有待处理的更改。
  2. 右键单击并选择“删除”,从解决方案中删除项目。
  3. 现在,在 Windows 资源管理器中,重命名项目文件夹。
  4. 返回 Visual Studio,在解决方案资源管理器中,右键单击解决方案并选择添加 -> 现有项目。 选择您在步骤 2 中删除的项目的项目文件,该文件应位于重命名的文件夹中。
  5. 现在该项目已回到解决方案中,但该项目似乎并未添加到源代码管理中。 要解决此问题,请打开源代码管理资源管理器。
  6. 在源代码管理资源管理器中找到与您在步骤 3 中重命名的磁盘上的项目文件夹相对应的项目文件夹
  7. 。在源代码管理资源管理器中重命名该文件夹,使其与磁盘上的项目文件夹具有相同的名称。
  8. 现在看看您的待处理更改。 您应该对解决方案文件进行更改并对项目文件夹进行重命名操作。
  9. 进行重建并确保一切都正确编译。 如果您对重命名的项目有项目间引用,则需要将它们再次添加到引用它的各个项目中。
  10. 现在你应该已经准备好了。 去检查所有内容。

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

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

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

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

(Make sure you read @mjv's comment below, as he notes that you can skip step 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.

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.

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.

淡淡の花香 2024-07-13 16:01:01

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

瞧:)

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à :)

走走停停 2024-07-13 16:01:01

更简单的解决方案如下:

  1. 右键单击项目并重命名。
  2. (可选)打开项目的属性设置并修改程序集名称(以及可选的默认命名空间)以使用新的项目名称。
  3. (可选)选择源文件中的命名空间名称,右键单击并选择重构/重命名将命名空间全局重命名为新项目名称。
  4. (可选)打开 AssemblyInfo.cs 文件并更改程序集名称以匹配。
  5. 保存并关闭解决方案。
  6. 使用 Windows 资源管理器,将项目文件夹重命名为新名称。
  7. 在文本编辑器中打开 SLN 文件,找到对项目路径的一个引用,并将其更改为使用新的文件夹名称。

有四个必要步骤,但建议七个步骤。 但最终该项目已完全重命名。 从技术上讲,项目的文件夹名称不必与项目本身匹配,因此即使该步骤也是可选的,但如果它们不匹配,则可能会造成混乱。 程序集和命名空间名称也是如此。

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/Rename to 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.

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.

め可乐爱微笑 2024-07-13 16:01:01

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

  1. 重命名该项目。
  2. 关闭解决方案(保存)。
  3. 重命名 Visual Studio 外部的文件夹。
  4. 打开解决方案,忽略警告。
  5. 浏览所有不可用的项目并将属性“文件路径”设置为项目文件的新位置,即 someproject.csproj
  6. 重新加载项目。

此外,执行这些步骤后,您可能需要将其他引用重命名为旧项目名称。

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

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

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

...并将消除错误“命名空间与文件位置不对应,应该是:‘SomeProjectName’”

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

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

In andersjanmyr's answer it's easier to rename the project first.

  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.

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'"

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

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

﹂绝世的画 2024-07-13 16:01:01

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

  1. 关闭 Visual Studio

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

  3. 使用 Git 移动命令,如下所示:

    git mv ;   <新文件夹名称> 
      

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

  4. 在 .sln 文件中:找到定义项目的行并更改路径中的文件夹名称。 该行应类似于:

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

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

    我读到 ReSharper 为此提供了一些选项。 但简单的查找/替换就完成了我的工作。

  7. 删除旧的 .gitignore 路径。

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

  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 ReSharper has some options for this. But simple find/replace did the job for me.

  7. Remove old .gitignore paths.

oО清风挽发oО 2024-07-13 16:01:01

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

下载

它将在以下位置重命名项目:

  • 项目位置文件夹
  • 解决方案文件
  • 其他项目中的引用 程序
  • 集名称、信息
  • 默认命名空间

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

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
2024-07-13 16:01:01

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

  1. 关闭 Visual Studio 项目。
  2. 打开 .sln 文件并重命名每个 .csproj 出现的项目目录前缀。
  3. 保存 .sln 文件
  4. 重命名硬盘驱动器上的实际项目文件夹目录,以匹配 .sln 文件中的更改。
  5. 打开 .sln(在 Visual Studio 中)并重建

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

  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
无所谓啦 2024-07-13 16:01:01

使用 Visual Studio 2019,我按照以下步骤成功更改项目名称:

  1. 关闭解决方案
  2. 重命名项目文件夹以与新项目名称匹配
  3. 在记事本 ++ 类型的编辑器中打开解决方案文件,并使用新项目名称文件夹编辑 FilePath
  4. 打开解决方案如果询问是否要从源代码管理打开,请单击“否”
  5. 右键单击要重命名的项目,然后单击“属性”,然后进行以下更改:
    更改程序集名称,
    默认程序集命名空间和
    具有新名称的程序集信息
  6. 打开任何文件并将文件移动到新的命名空间,这将由所有文件完成
  7. 如果您有 app.config 类型的文件,那么请确保将它们也移动到新的命名空间中
  8. 重新构建它将成功工作

Using Visual Studio 2019, I followed below steps to make the project name change successful:

  1. Close the solution
  2. Rename the project folder to match with new project name
  3. Open solution file in notepad++ kind of editor and edit the FilePath with new project name folder
  4. Open the solution and click No if it ask whether you want to open from source control
  5. Right click the project which you want renaming and click Properties then change below:
    Change Assembly Name,
    Default Assembly namespace and
    Assembly information with new name
  6. Open any of the file and move the file to new namespace which will be done by all files
  7. If you have app.config kind of files then make sure to move them also in new namespace
  8. Rebuild it which will work successfully
泪是无色的血 2024-07-13 16:01:01

商业智能数据工具 (SSDT-BI 2013) 的 Visual Studio 扩展的经过验证的解决方案:

  1. 将项目文件夹移动到新位置(暂时不要重命名任何内容)
  2. 在解决方案资源管理器中,重命名项目/解决方案。
  3. 关闭(并保存)新命名的项目。
  4. 重命名项目的文件夹和 .sln 文件。
  5. 使用文本编辑器并打开新重命名的项目解决方案文件 (.sln)(我使用 Notepad++
  6. 在第 6 行中,您将看到:“Project {fdjfksdjk4387!...} = "OLDPROJECT", "OLDFOLDER\OLDPROJECT.rptproj"。使用步骤 4 中使用的新名称重命名所有内容。(即... = "NEWPROJECT", "NEWFOLDER\NEWPROJECT.rptproj"... )
  7. 就是这样!

它经过了 100% 的测试,在我的情况下完美运行。

注意:我无法确认它是否可以在不同的项目模板和其他模板下工作。 Visual Studio 版本。与往常一样,请提前备份所有内容。

A proven solution for Visual Studio extension for Data Tools for Business Intelligence (SSDT-BI 2013):

  1. Move the Project folder to its new location (don't rename anything yet)
  2. In Solution Explorer, rename the Project / Solution.
  3. Close (and save) the newly-renamed project.
  4. Rename the project's folder and .sln file.
  5. Use a text editor and open the newly-renamed project solution file (.sln) (I used Notepad++)
  6. In line number 6 you'll see: "Project {fdjfksdjk4387!...} = "OLDPROJECT", "OLDFOLDER\OLDPROJECT.rptproj". Rename everything with the new names used in step 4. (i.e. ... = "NEWPROJECT", "NEWFOLDER\NEWPROJECT.rptproj"... )
  7. That's it!

It was tested 100% and worked flawlessly in my case.

NOTE: I can't confirm if it works under different project templates and other Visual Studio versions. As always, do backup everything beforehand.

空城缀染半城烟沙 2024-07-13 16:01:01

我刚刚为自己编写了一个全局 dotnet 工具(也考虑了 git+history)解决了这个问题。

安装通过
dotnet tool install -g ModernRonin.ProjectRenamer,与 renameproject 一起使用 <新名称>

文档/修补/PR 位于

https://github.com/ModernRonin/ProjectRenamer

I just solved this problem for myself writing a global dotnet tool (that also takes into account git+history).

Install via
dotnet tool install -g ModernRonin.ProjectRenamer, use with renameproject <oldName> <newName>.

Documentation/Tinkering/PRs at

https://github.com/ModernRonin/ProjectRenamer

走野 2024-07-13 16:01:01

Visual Studio 2017 中对我有用的内容:

  • 在 Visual Studio 中关闭解决方案
  • 重命名解决方案中的项目目录。
    • (在源代码管理中推送更改 - 在我的例子中是 Git)
  • 在文本编辑器(Visual Studio 2017 外部)中编辑 .sln 文件,更改目录名称。
  • 在 Visual Studio 中重新打开解决方案

它显示类似“重新添加项目”之类的内容。 我重建了一切,一切都很顺利。

What worked for me in Visual Studio 2017:

  • Close solution in Visual Studio
  • Rename the directories of projects in the solution.
    • (push change in source control - Git in my case)
  • Edit the .sln file in a text editor (outside Visual Studio 2017) changing the name of the directory.
  • Reopen the solution in Visual Studio

It said something like "re-adding project". I rebuilt everything and everything was good to go.

勿忘心安 2024-07-13 16:01:01

我最近正在使用 VS2022 开发 .Net 6 项目。 我需要将 templatemicroservice 重命名为 UserMicroservice。

.Net 6 为您改变了大部分事情。 例如程序集名称和命名空间。 此外,它还会自动更改 .sln 中的 ProjectName 引用。 因此,这个答案将真正帮助您毫无障碍地重命名您的服务。

在此处输入图像描述

所以,我遵循的步骤:

在 Visual Studio 中打开解决方案并执行以下步骤:

  1. 我通过右键单击解决方案来重命名它。 例如,TemplateServiceSolutionUserServiceSolution
  2. 通过右键单击解决方案中的所有项目来重命名它们。 例如,TemplateServiceUserService
  3. 在一个特定的项目中,我搜索了以下命名空间:namespace TemplateService,并将所有命名空间替换为namespace UserService。 在每个项目中执行此步骤。 还将using TemplateService 替换为using UserService
  4. 打开您的 launchsettings.json,并重命名与旧服务相关的任何内容,因为我在 launchsettings.json 的配置文件中几乎没有什么内容。
  5. 为了确定起见,请同时检查您的startup 和program.cs 文件,我没有对它们进行任何更改。

从 Visual Studio 关闭解决方案并执行以下步骤:

  1. 关闭解决方案,然后打开文件资源管理器。 将我的所有项目文件夹从 TemplateService 重命名为 UserService
  2. 在记事本/记事本++中打开.sln,并且必须更改文件夹结构名称。 就像对于特定项目一样,我应该给出正确的 .csproj 路径。 如图所示,我需要将 TemplateService 重命名为 UserService
    输入图片此处描述

在 Visual Studio 中打开解决方案。 当您在 Visual Studio Code 中加载解决方案时,大多数依赖项都会被加载。 你已经准备好了。

I am recently working on .Net 6 project with VS2022. and I need to rename my templatemicroservice to my UserMicroservice.

.Net 6 changes most of the things for you. like AssemblyNames and NameSpaces. Also, it changes the ProjectName references in .sln automatically. So, this answer will really help you to rename your service with no hurdles.

enter image description here

So, Steps I followed:

Open the solution in Visual Studio and did these steps:

  1. I renamed my solution by right clicking on it. like, TemplateServiceSolution to UserServiceSolution.
  2. Renamed all the Projects in solution by right clicking on them. like, TemplateService to UserService.
  3. In a specific project, I searched the namespaces like: namespace TemplateService and replaced all by namespace UserService. Do this step in each project. Also relplace using TemplateService to using UserService.
  4. Open your launchsettings.json, and rename anything related to old service, as I had few things in profiles of launchsettings.json.
  5. Just to be sure, please check your startup and program.cs files as well, I didn't have any changes in them.

Closed the solution from Visual Studio and did these steps:

  1. Closed the solution, and opened the File Explorer. Renamed all my project folders from TemplateService to UserService.
  2. Open the .sln in notepad/notepad++, and must change the Folder Structure name. like for a particular project, I should give the correct .csproj paths. as shown in image, I need to rename TemplateService to UserService.
    enter image description here

Open the solution in Visual Studio. Most of your Dependencies will be loaded as soon as you Load your solution in Visual Studio Code. and you are ready.

或十年 2024-07-13 16:01:01

请参阅链接中的第 3 项文章

  1. 关闭解决方案和 IDE。
  2. 在 Windows 资源管理器中:将目录名称更改为新名称。
  3. 在 Windows 资源管理器中:使用文本编辑器打开 .sln 文件。
  4. 将目录名称更改为新名称并保存。
  5. 如果解决方案没有自动启动,请重新启动 IDE 并从菜单“文件”→“最近的文件”菜单打开解决方案。
  6. 单击解决方案资源管理器中的项目文件夹,然后检查底部属性中的路径属性。 现在它将引用新的项目文件夹。

这对我有用。

See item 3 in the linked article.

  1. Close the solution and the IDE.
  2. In Windows Explorer: Change the directory name to the new name.
  3. In Windows Explorer: Open the .sln file with a text editor.
  4. Change the directory name to the new name and save.
  5. Restart the IDE and open the solution from menu FileRecent Files menu if it doesn't start automatically.
  6. Click on the project folder in Solution Explorer and check the path property in the properties at the bottom. It will now be referencing to the new project folder.

It worked for me.

轻拂→两袖风尘 2024-07-13 16:01:01

我不得不这样做很多次。 能够重新调整现有项目的用途,并且能够重命名命名空间、文件和文件夹中的文本(包括文件/目录名称),这非常有用。

使用从根文件夹开始的递归查找和替换意味着重命名不会破坏解决方案文件和项目引用中项目的链接。

为此,我创建了一个项目来做到这一点。 该应用程序还努力忽略版本控制文件夹,例如 .git、.svn 和 .vs 设置文件。 更多信息位于自述文件中。

https://github.com/garethrbrown/vs-project-rename

示例

I've had to do this lots of times. It's really useful to be able to repurpose an existing project, but be able to rename text in namespaces, files, and folders (including file / directory names).

Using a recursive find and replace starting at the root folder means the rename doesn't break links to projects in the solution files and project references.

To this end, I have created a project to do just this. The application also makes an effort to ignore version control folders such as .git, .svn and the .vs settings file. More information is in the README.

https://github.com/garethrbrown/vs-project-rename

Example

心如荒岛 2024-07-13 16:01:01

注意:此修复适用于 Visual Studio 2008,但在这里应该有效。

  1. 使用 Windows 资源管理器,将解决方案文件夹(父文件夹和子文件夹)重命名为新的解决方案名称。
  2. 删除位于父文件夹中的 .sln 文件。
  3. 在 Visual Studio 中,选择菜单文件打开项目
  4. 深入到刚刚重命名的新文件夹并打开 .csproj 文件(位于子文件夹中)。
  5. 右键单击项目名称并将其重命名为您想要的名称。 (它应该与步骤 1 中的文件夹名称相同。)
  6. 选择菜单文件关闭解决方案。 将出现一个对话框,询问您是否要将更改保存到 .sln 文件。 单击
  7. 在“文件另存为”对话框中,指向新重命名的“父文件夹”,然后单击“保存”。
    (注意:确保 .sln 文件与文件夹同名。这不是必需的,但可以保持一致性。)

完成。

Note: This fix is for Visual Studio 2008, but it should work here.

  1. Using Windows Explorer, rename both the solution folders (the parent folder and the child folder) to the new solution name.
  2. Delete the .sln file located in the parent folder.
  3. In Visual Studio, select menu FileOpen Project.
  4. Drill into the new folder you just renamed and open the .csproj file (located in the child folder).
  5. Right-click the project name and rename it to what you want. (It should be the same name as the folder in step 1.)
  6. Select menu FileClose Solution. A dialog will ask if you want to save changes to the .sln file. Click Yes.
  7. In the Save File As dialog, point to the newly renamed parent folder and click Save.
    (Note: Make sure the .sln file has the same name as the folder. It is not required, but it maintains consistency.)

Done.

葮薆情 2024-07-13 16:01:01

这在 Visual Studio 2019 中对我来说效果很好。

  1. 将解决方案、Visual Studio 中的项目重命名为简单的单个
    像平常一样单击文件名。
  2. 在 Visual Studio 中重命名命名空间。
  3. 重命名主项目页面上所需的元素(发布位置、应用程序、默认命名空间等)。 如正确所述,这对文件夹没有任何作用,但它会重命名项目文件并将所有内容在 Visual Studio 中捆绑在一起。
  4. 关闭视觉工作室。
  5. 重命名文件夹。
  6. 打开 Visual Studio 并回答“否”以从源代码管理获取项目。
  7. 删除已卸载的项目引用。
  8. 使用从解决方案添加现有项目将每个项目添加回来。 这将所有内容和为我构建的项目联系在一起。

请评论并纠正以上任何不起作用的内容。

This worked well for me in Visual Studio 2019.

  1. Rename the solution, projects in Visual Studio by simply single
    clicking on the file names as normal.
  2. Rename the namespaces in Visual Studio.
  3. Rename the desired elements on the main project page (publish location, application, default namespace, whatever). As noted correctly, this does nothing for the folders, but it does rename the project files and keeps everything tied together in Visual Studio.
  4. Close Visual Studio.
  5. Rename the folders.
  6. Open Visual Studio and reply 'no' to getting projects from Source Control.
  7. Delete the unloaded project references.
  8. Add each project back in using Add existing project from Solution. This tied everything together and the project built for me.

Please comment on and correct anything above that does not work.

神回复 2024-07-13 16:01:01

这就是我在 VS19 中重命名现有项目的方法。

  1. 关闭 Visual Studio 项目
  2. 重命名并打开驱动器上的每个项目文件夹名称
  3. 重命名每个 .csproj
  4. 打开 .sln 文件并重命名每个 .csproj 出现的项目目录前缀。
  5. 在 Visual Studio 中打开 .sln 文件并
  6. 通过遍历每个文件或使用 ReplaceAll 重建更新先前的名称空间引用与新的名称空间引用

This is how I renamed my existing project in VS19.

  1. Close the visual studio project
  2. Rename and open the each project folder name on your drive
  3. Rename each .csproj
  4. Open .sln file and rename the project directory prefix for each of the .csproj occurrences.
  5. Open the .sln file in visual studio and rebuild
  6. Update prevoius nameSpace refrence with new one by going through each file or using ReplaceAll
讽刺将军 2024-07-13 16:01:01

如果项目当前正在运行,则无法重命名文件夹

  1. 停止应用程序 Shift+F5
  2. 从解决方案资源管理器重命名文件夹(右-单击>重命名F2

You cannot rename a folder if your project is currently running

  1. Stop your application Shift+F5
  2. Rename your folder from the Solution Explorer (right-click > rename or F2)
给不了的爱 2024-07-13 16:01:01

我编写了一个小工具来自动执行所有这些步骤。 目前它还支持 Subversion

有关当前版本的信息,请访问 Visual Studio 项目重命名器信息 >。

现在可以从 Visual Studio 项目重命名器下载页面 下载最新版本。

非常感谢您的反馈。

I have written a small tool that automates all these steps. It also supports Subversion for now.

Information about current releases can be found at Visual Studio Project Renamer Infos.

The latest releases can now be downloaded from the Visual Studio Project Renamer Download Page.

Feedback is much appreciated.

生来就爱笑 2024-07-13 16:01:01

我经常遇到同样的问题,即在 Visual Studio 中重命名项目并编辑文件夹名称、项目名称和 .sln 文件以实现此目的。 我刚刚编写了一个 VBScript 脚本来完成这一切。 您必须小心选择要替换的字符串。

您只需将 .vbs 文件放在与解决方案的 .sln 文件相同的目录中即可。

' Script parameters'
Solution = "Rename_Visual_Studio_Project" '.sln'
Project = "Rename_Visual_Studio_Project" '.csproj'
NewProject = "SUCCESS"

Const ForReading = 1
Const ForWriting = 2

Set objFso = CreateObject("Scripting.FileSystemObject")
scriptDirr = objFso.GetParentFolderName(wscript.ScriptFullName)

' Rename the all project references in the .sln file'
Set objFile = objFso.OpenTextFile(scriptDirr + "\" + Solution + ".sln", ForReading)
fileText = objFile.ReadAll
newFileText = Replace(fileText, Project, NewProject)
Set objFile = objFSO.OpenTextFile(scriptDirr + "\" + Solution + ".sln", ForWriting)
objFile.WriteLine(newFileText)
objFile.Close

' Rename the .csproj file'
objFso.MoveFile scriptDirr + "\" + Project + "\" + Project + ".csproj", scriptDirr + "\" + Project + "\" + NewProject + ".csproj"

' Rename the folder of the .csproj file'
objFso.MoveFolder scriptDirr + "\" + Project, scriptDirr + "\" + NewProject

I often had the same problem of renaming a project in Visual Studio and editing the folder name, project name, and .sln file in order to accomplish that. I just wrote a VBScript script that accomplishes all that. You have to be careful with the strings you choose for replacing.

You just have to put the .vbs file in the same directory as the .sln file of the solution.

' Script parameters'
Solution = "Rename_Visual_Studio_Project" '.sln'
Project = "Rename_Visual_Studio_Project" '.csproj'
NewProject = "SUCCESS"

Const ForReading = 1
Const ForWriting = 2

Set objFso = CreateObject("Scripting.FileSystemObject")
scriptDirr = objFso.GetParentFolderName(wscript.ScriptFullName)

' Rename the all project references in the .sln file'
Set objFile = objFso.OpenTextFile(scriptDirr + "\" + Solution + ".sln", ForReading)
fileText = objFile.ReadAll
newFileText = Replace(fileText, Project, NewProject)
Set objFile = objFSO.OpenTextFile(scriptDirr + "\" + Solution + ".sln", ForWriting)
objFile.WriteLine(newFileText)
objFile.Close

' Rename the .csproj file'
objFso.MoveFile scriptDirr + "\" + Project + "\" + Project + ".csproj", scriptDirr + "\" + Project + "\" + NewProject + ".csproj"

' Rename the folder of the .csproj file'
objFso.MoveFolder scriptDirr + "\" + Project, scriptDirr + "\" + NewProject
红衣飘飘貌似仙 2024-07-13 16:01:01
  1. 重命名解决方案中的项目和项目文件夹

  2. 从解决方案中删除项目

  3. 添加将现有项目添加到解决方案(您重命名的项目)

它对我有用。 TFS 还将跟踪新项目。

  1. Rename the project in the solution and the project folder

  2. Delete the project from the solution

  3. Add the existing project to the solution (your renamed project)

It works for me. TFS will also track the new project.

眼眸里的那抹悲凉 2024-07-13 16:01:01

转到 .sln 文件,右键单击,使用 Notepad++(或任何编辑器;Notepad++ 是最快的)打开,找到路径,更改它。

Go to the .sln file, right click, open with Notepad++ (or any editor; Notepad++ is the fastest), find the path, change it.

带刺的爱情 2024-07-13 16:01:01

使用 TFS 时,步骤 2 实际上是在源代码管理中重命名文件夹,然后在重新打开解决方案之前获取最新版本。

When using TFS, step 2 is actually to rename the folder in source control and then get the latest before reopening the solution.

临风闻羌笛 2024-07-13 16:01:01

有一种更简单的方法,已在 Visual Studio 2013 Update 1 中进行了测试,适用于 TFS 连接的项目:

  • 打开 Visual Studio,但关闭解决方案。
  • 打开源资源管理器并重命名所需的文件夹(如果您尚未先关闭解决方案,该解决方案将为您关闭)。
  • 右键单击解决方案(也可以从源资源管理器)并选择签入。
  • 打开解决方案。 您会被告知添加了新项目。 接受以获取更改。
  • 从解决方案中删除该项目并再次添加,然后签入。
  • 检查内部参考是否正常。

There's a simpler approach which was tested in Visual Studio 2013 Update 1 and applicable for TFS-connected projects:

  • Open Visual Studio, but let the solution be closed.
  • Open Source Explorer and rename the desired folder (the solution will be closed for you if you didn't already close it first).
  • Right-click on the solution (from Source Explorer also) and select check-in.
  • Open the solution. You'll be told that there're new projects added. Accept to get the changes.
  • Remove the project from the solution and add it again, and then check-in.
  • Check that the internal references are OK.
昨迟人 2024-07-13 16:01:01

我们最近上传了一个免费的 Visual Studio 扩展的测试版,它可以为您完成这些工作。

查看 Visual Studio Gallery:图库下载

We recently uploaded a beta of a free Visual Studio extension which does this stuff for you.

Have a look at Visual Studio Gallery: Gallery Download

落日海湾 2024-07-13 16:01:01

好吧,我按照自己的方式进行了

  • 关闭 Visual Studio 2012
  • 将子目录重命名为 .sln 下的首选名称
  • 删除 *.suo 文件
  • 再次打开解决方案,并修复加载的项目的任何属性以满足新的子目录名称

Well, I did it my way

  • Close Visual Studio 2012
  • Rename your subdirectory to the preferred name under .sln
  • Delete the *.suo file
  • Open the solution again, and fix any properties of Project(s) loaded to meet the new subdirectory name
你与清晨阳光 2024-07-13 16:01:01
  1. 在 Visual Studio 之外重命名该项目。
  2. 使用文本编辑器编辑your_project_name.sln,并将路径重命名为新路径。
  1. Rename the project outside Visual Studio.
  2. Edit your_project_name.sln with a text editor, and rename the path to the new path.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文