如何在 Visual Studio 中重命名项目文件夹?
我当前重命名项目文件夹的解决方案是:
- 从解决方案中删除项目。
- 在 Visual Studio 外部重命名该文件夹。
- 将项目重新添加到解决方案中。
有没有更好的办法?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我当前重命名项目文件夹的解决方案是:
有没有更好的办法?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(30)
要在 Visual Studio 中重命名项目的文件夹、文件 (
.*proj
) 和显示名称:注意:其他建议的涉及删除项目然后将项目重新添加到解决方案的解决方案将破坏项目引用。
如果执行这些步骤,那么您还可以考虑重命名以下内容以匹配:
另请考虑修改以下值 程序集属性:
AssemblyProductAttribute
AssemblyDescriptionAttribute
AssemblyTitleAttribute
To rename a project's folder, file (
.*proj
) and display name in Visual Studio:.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.)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:
Also consider modifying the values of the following assembly attributes:
AssemblyProductAttribute
AssemblyDescriptionAttribute
AssemblyTitleAttribute
这在 Visual Studio 2015 中很简单(可能适用于旧版本)
.sln
文件,并更改csproj 的路径,即fu\bar.csproj
→bar\bar.csproj
。This is straightforward in Visual Studio 2015 (possibly works in older versions)
.sln
file in Notepad, and change the path to the csproj, i.e.,fu\bar.csproj
→bar\bar.csproj
.还有另一种方法可以做到这一点,即使用 *.sol、*csproj 文件。
它将像这样(相对于 *.sol 文件):
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Shani.Commands.Impl", "Shani.Commands.Impl\Shani.Commands.Impl.csproj", "{747CFA4B-FC83-419A-858E-5E2DE2B948EE}"
只需将第一部分更改为新目录即可,例如:
Impl\Shani.Commands.Impl\Shani.Commands.Impl.csproj
当然,不要忘记将整个项目移动到该目录。
There is another way doing this, using the *.sol, *csproj files.
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}"
And just change the first part to the new diretory for example:
Impl\Shani.Commands.Impl\Shani.Commands.Impl.csproj
Of course, don't forget to move the whole project to that directory.
伙计,我为此苦苦挣扎吗? 遗憾的是,Visual Studio 中没有一键式解决方案,但如果您运行的是 Visual Studio 2012,并且您的项目受 Team Foundation Server 的源代码控制,则这里是我如何让它工作,同时保留源代码历史记录:
(请确保您阅读下面 @mjv 的评论,因为他指出您可以跳过步骤 5-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)
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.
目前,没有。 好吧,实际上您可以单击损坏的项目节点,然后在属性窗格中查找属性“路径”,单击小浏览图标,然后选择新路径。
瞧:)
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à :)
更简单的解决方案如下:
有四个必要步骤,但建议七个步骤。 但最终该项目已完全重命名。 从技术上讲,项目的文件夹名称不必与项目本身匹配,因此即使该步骤也是可选的,但如果它们不匹配,则可能会造成混乱。 程序集和命名空间名称也是如此。
The simpler solution is the following:
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.
在andersjanmyr的回答 首先重命名项目会更容易。
此外,执行这些步骤后,您可能需要将其他引用重命名为旧项目名称。
在项目属性中,更新程序集名称和默认命名空间。
这将更新项目文件中的以下内容...
...并将消除错误“命名空间与文件位置不对应,应该是:‘SomeProjectName’”
重命名您的根命名空间(如果您有 ReSharper,右键单击命名空间并进行重构 -> 重命名)。
更改 AssemblyInfo.cs 中所有出现的旧项目名称。
In andersjanmyr's answer it's easier to rename the project first.
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...
...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.
对于使用 Visual Studio + Git 并希望保留文件历史记录的用户(可以重命名项目和/或解决方案):
关闭 Visual Studio
在 .gitignore 文件中,复制所有忽略您要使用这些路径的重命名版本重命名的项目的路径。
使用 Git 移动命令,如下所示:
有关其他选项,请参阅文档:https://git-scm.com/docs/git- mv
在 .sln 文件中:找到定义项目的行并更改路径中的文件夹名称。 该行应类似于:
打开 Visual Studio,然后右键单击项目 → 重命名
然后,重命名命名空间。
我读到 ReSharper 为此提供了一些选项。 但简单的查找/替换就完成了我的工作。
删除旧的 .gitignore 路径。
For those using Visual Studio + Git and wanting to keep the file history (works renaming both projects and/or solutions):
Close Visual Studio
In the .gitignore file, duplicate all ignore paths of the project you want to rename with renamed versions of those paths.
Use the Git move command like this:
See documentation for additional options: https://git-scm.com/docs/git-mv
In your .sln file: Find the line defining your project and change the folder name in path. The line should look something like:
Open Visual Studio, and right click on project → Rename
Afterwards, rename the namespaces.
I read that ReSharper has some options for this. But simple find/replace did the job for me.
Remove old .gitignore paths.
对于 Visual Studio 2017,您可以使用我的 Visual Studio 扩展:
下载
它将在以下位置重命名项目:
For Visual Studio 2017 you can use my Visual Studio extension:
Download
It will rename the project in:
我只需自己完成此操作(使用 Visual Studio 2010)。 正如一些人回答的那样,最简单的步骤似乎是:
I just had to do this myself (using Visual Studio 2010). As some folks have answered, the simplest step seems to be:
使用 Visual Studio 2019,我按照以下步骤成功更改项目名称:
更改程序集名称,
默认程序集命名空间和
具有新名称的程序集信息
Using Visual Studio 2019, I followed below steps to make the project name change successful:
Change Assembly Name,
Default Assembly namespace and
Assembly information with new name
商业智能数据工具 (SSDT-BI 2013) 的 Visual Studio 扩展的经过验证的解决方案:
它经过了 100% 的测试,在我的情况下完美运行。
注意:我无法确认它是否可以在不同的项目模板和其他模板下工作。 Visual Studio 版本。与往常一样,请提前备份所有内容。
A proven solution for Visual Studio extension for Data Tools for Business Intelligence (SSDT-BI 2013):
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.
我刚刚为自己编写了一个全局 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 withrenameproject <oldName> <newName>
.Documentation/Tinkering/PRs at
https://github.com/ModernRonin/ProjectRenamer
Visual Studio 2017 中对我有用的内容:
它显示类似“重新添加项目”之类的内容。 我重建了一切,一切都很顺利。
What worked for me in Visual Studio 2017:
It said something like "re-adding project". I rebuilt everything and everything was good to go.
我最近正在使用 VS2022 开发 .Net 6 项目。 我需要将 templatemicroservice 重命名为 UserMicroservice。
.Net 6 为您改变了大部分事情。 例如程序集名称和命名空间。 此外,它还会自动更改 .sln 中的 ProjectName 引用。 因此,这个答案将真正帮助您毫无障碍地重命名您的服务。
所以,我遵循的步骤:
TemplateServiceSolution
到UserServiceSolution
。TemplateService
到UserService
。namespace TemplateService
,并将所有命名空间替换为namespace UserService
。 在每个项目中执行此步骤。 还将using TemplateService
替换为using UserService
。launchsettings.json
,并重命名与旧服务相关的任何内容,因为我在launchsettings.json
的配置文件中几乎没有什么内容。TemplateService
重命名为UserService
。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.
So, Steps I followed:
TemplateServiceSolution
toUserServiceSolution
.TemplateService
toUserService
.namespace TemplateService
and replaced all bynamespace UserService
. Do this step in each project. Also relplaceusing TemplateService
tousing UserService
.launchsettings.json
, and rename anything related to old service, as I had few things in profiles oflaunchsettings.json
.TemplateService
toUserService
.TemplateService
toUserService
.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.
请参阅链接中的第 3 项文章。
这对我有用。
See item 3 in the linked article.
It worked for me.
我不得不这样做很多次。 能够重新调整现有项目的用途,并且能够重命名命名空间、文件和文件夹中的文本(包括文件/目录名称),这非常有用。
使用从根文件夹开始的递归查找和替换意味着重命名不会破坏解决方案文件和项目引用中项目的链接。
为此,我创建了一个项目来做到这一点。 该应用程序还努力忽略版本控制文件夹,例如 .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
注意:此修复适用于 Visual Studio 2008,但在这里应该有效。
.sln
文件。.sln
文件。 单击是。(注意:确保
.sln
文件与文件夹同名。这不是必需的,但可以保持一致性。)完成。
Note: This fix is for Visual Studio 2008, but it should work here.
.sln
file located in the parent folder..sln
file. Click Yes.(Note: Make sure the
.sln
file has the same name as the folder. It is not required, but it maintains consistency.)Done.
这在 Visual Studio 2019 中对我来说效果很好。
像平常一样单击文件名。
请评论并纠正以上任何不起作用的内容。
This worked well for me in Visual Studio 2019.
clicking on the file names as normal.
Please comment on and correct anything above that does not work.
这就是我在 VS19 中重命名现有项目的方法。
This is how I renamed my existing project in VS19.
如果项目当前正在运行,则无法重命名文件夹
右-单击>重命名
或F2)You cannot rename a folder if your project is currently running
right-click > rename
or F2)我编写了一个小工具来自动执行所有这些步骤。 目前它还支持 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.
我经常遇到同样的问题,即在 Visual Studio 中重命名项目并编辑文件夹名称、项目名称和 .sln 文件以实现此目的。 我刚刚编写了一个 VBScript 脚本来完成这一切。 您必须小心选择要替换的字符串。
您只需将 .vbs 文件放在与解决方案的 .sln 文件相同的目录中即可。
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.
重命名解决方案中的项目和项目文件夹
从解决方案中删除项目
添加将现有项目添加到解决方案(您重命名的项目)
它对我有用。 TFS 还将跟踪新项目。
Rename the project in the solution and the project folder
Delete the project from the solution
Add the existing project to the solution (your renamed project)
It works for me. TFS will also track the new project.
转到 .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.
使用 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.
有一种更简单的方法,已在 Visual Studio 2013 Update 1 中进行了测试,适用于 TFS 连接的项目:
There's a simpler approach which was tested in Visual Studio 2013 Update 1 and applicable for TFS-connected projects:
我们最近上传了一个免费的 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
好吧,我按照自己的方式进行了
Well, I did it my way