visual-studio Visual Studio 将项目移动到其他文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/2620027/
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
Visual Studio move project to a different folder
提问by Egor Pavlikhin
How do I move a project to a different folder in Visual Studio? I am used to this structure in my projects.
如何将项目移动到 Visual Studio 中的其他文件夹?我在我的项目中习惯了这种结构。
-- app
---- Project.Something
---- Project.SomethingElse
I want to rename the whole namespace SomethingElse to SomethingNew, what's the best way to do that (without manually going into .sln file)?
我想将整个命名空间SomethingElse重命名为SomethingNew,最好的方法是什么(无需手动进入.sln文件)?
采纳答案by Hans Passant
Remove the project from your solution by right-clicking it in the Solution Explorer window and choosing Remove. Move the entire project folder, including subdirectories wherever you want it to go. Add the project back to your solution.
通过在“解决方案资源管理器”窗口中右键单击该项目并选择“删除”,将其从您的解决方案中删除。将整个项目文件夹(包括子目录)移动到您想要的任何位置。将该项目添加回您的解决方案。
Namespace names is something completely different, just edit the source code.
命名空间名称完全不同,只需编辑源代码即可。
回答by Igor ostrovsky
I tried the suggestion to remove and re-add the project, but then fixing up dependencies can be a pain.
我尝试了删除并重新添加项目的建议,但随后修复依赖项可能会很痛苦。
I use this approach:
我使用这种方法:
- Move the project folder.
- If the project is in source control, do the move using source control commands.
 
- Edit the solution file in a text editor. There should be only one path that you need to change.
- 移动项目文件夹。
- 如果项目在源代码管理中,请使用源代码管理命令进行移动。
 
- 在文本编辑器中编辑解决方案文件。您应该只需要更改一条路径。
回答by Emmanuel
- Close your solution in VS2012
- Move your project to the new location
- Open your solution
- Select the project that failed to load
- In the Properties tool window, there an editable “File path” entry that allows you to select the new project location
- Set the new path
- Right click on the project and click reload
- 在 VS2012 中关闭您的解决方案
- 将您的项目移动到新位置
- 打开您的解决方案
- 选择加载失败的项目
- 在“属性”工具窗口中,有一个可编辑的“文件路径”条目,允许您选择新的项目位置
- 设置新路径
- 右键单击项目,然后单击重新加载
回答by Gonzalo Méndez
What worked for me was to:
对我有用的是:
- Remove the project from the solution.
- Edit the project file with a text editor.
- Update all relative paths to the "packages". In my case I had to change ..\packagesto..\..\..\packagessince I moved the project to a deeper folder.
- Load the project back into the solution.
- 从解决方案中删除项目。
- 使用文本编辑器编辑项目文件。
- 更新“包”的所有相对路径。就我而言,我不得不更改..\packages为,..\..\..\packages因为我将项目移到了更深的文件夹。
- 将项目重新加载到解决方案中。
回答by Victor David Francisco Enrique
I had the same problem. I solved with move the references and in less than 15 minutes, without change the references.
我有同样的问题。我在不到 15 分钟的时间内解决了移动引用的问题,而没有更改引用。
For me the solution was simple:
对我来说,解决方案很简单:
- Move your files where you need.
- Delete the folder with name .vs. Must be as not visible folder.
- Open the solution file (.sln) using a simple editor like note or notepad++.
- Change the reference where your file is, using the following structure: if you put your project in the same folder remove the previous folder or the reference "..\"; if you put in a above folder add the reference "..\" or the name of the folder.
- Save the file with the changes.
- Open the project file (.csproj) and do the same, remove or add the reference.
- Save the changes.
- Open the solution file.
- 将文件移动到需要的地方。
- 删除名称为 .vs 的文件夹。必须是不可见的文件夹。
- 使用简单的编辑器(如 note 或 notepad++)打开解决方案文件 (.sln)。
- 使用以下结构更改文件所在的引用:如果您将项目放在同一文件夹中,请删除前一个文件夹或引用“..\”;如果您放入上述文件夹,请添加引用“..\”或文件夹名称。
- 保存更改后的文件。
- 打开项目文件 (.csproj) 并执行相同操作,删除或添加引用。
- 保存更改。
- 打开解决方案文件。
Examples:
例子:
In solution file (.sln)
在解决方案文件 (.sln) 中
- Original: Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PATH1.UI", "ScannerPDF\PATH1.UI\PATH1.UI.csproj", "{A26438AD-E428-4AE4-8AB8-A5D6933E2D7B}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PATH1.DataService", "ScannerPDF\PATH1.DataService\PATH1.DataService.csproj", "{ED5A561B-3674-4613-ADE5-B13661146E2E}" - New: Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PATH1.MX.UI", "PATH1.MX.UI\PATH1.UI.csproj", "{A26438AD-E428-4AE4-8AB8-A5D6933E2D7B}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PATH1.DataService", "PATH1.DataService\PATH1.DataService.csproj", "{ED5A561B-3674-4613-ADE5-B13661146E2E}" 
- 原文: Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PATH1.UI", " ScannerPDF\PATH1.UI\PATH1.UI.csproj", "{A26438AD-E428-4AE4-8AB8-A5D7B3 }" 项目("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PATH1.DataService", " ScannerPDF\PATH1.DataService\PATH1.DataService.csproj", "{ED5A561B-3674-4613-ADE5-B16E }" - 新: 项目(“{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}”)=“PATH1.MX.UI”,“PATH1.MX.UI\PATH1.UI.csproj”,“{A26438AD-E428-4AE4-8AB8 -A5D6933E2D7B}" 项目("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PATH1.DataService", "PATH1.DataService\PATH1.DataService.csproj", "{ED5A561B-31376E16E14B-00C04F79EFBC}" }" 
In project file:
在项目文件中:
- Original: - New: - Original reference: ....\lib\RCWF\2018.1.220.40\TelerikCommon.dll - New reference: ..\lib\RCWF\2018.1.220.40\TelerikCommon.dll 
- 原来的: - 新的: - 原文参考: ....\lib\RCWF\2018.1.220.40\TelerikCommon.dll - 新参考: ..\lib\RCWF\2018.1.220.40\TelerikCommon.dll 
回答by dthal
Summary: rename-and-move in VS2019 with git, retaining git history, leveraging R# a bit, automatic dependent project reference updating (important for sln's with many projects, we have >200)
总结:在 VS2019 中使用 git 重命名和移动,保留 git 历史,稍微利用 R#,自动更新依赖项目引用(对于有很多项目的 sln 很重要,我们有 >200)
I have been using the following steps to rename-and-move C# projects in Visual Studio 2019. This process uses R# to adjust namespaces. The git history is retained by doing a "git mv" (avoiding add/delete history drop).
我一直在使用以下步骤在 Visual Studio 2019 中重命名和移动 C# 项目。此过程使用 R# 来调整命名空间。通过执行“git mv”(避免添加/删除历史记录)来保留 git 历史记录。
Two phases: 1) rename the project in place and 2) move the project.
两个阶段:1) 就地重命名项目和 2) 移动项目。
(Uses tip from base2re unloading projects.)
(使用来自base2重新卸载项目的提示。)
Rename
改名
- VS | Solution Explorer | right-click project | Rename (e.g., Utils.Foo to Foo).
- VS | Solution Explorer | right-click project | Properties | change assembly name, default namespace and Assembly Information fields
- Do 1 and 2 for corresponding test project (e.g., Utils.Foo.Tests)
- VS | Solution Explorer | right-click projects (production and test) | Refactor | Adjust Namespaces
- XAML files that use the project may need to be updated (manually or with an appropriate global search and replace)
- Rebuild All
- Commit!! (to commit changes before moves)
- VS | 解决方案资源管理器 | 右键单击项目| 重命名(例如,Utils.Foo 到 Foo)。
- VS | 解决方案资源管理器 | 右键单击项目| 属性 | 更改程序集名称、默认命名空间和程序集信息字段
- 对相应的测试项目(例如,Utils.Foo.Tests)执行 1 和 2
- VS | 解决方案资源管理器 | 右键单击项目(生产和测试)| 重构 | 调整命名空间
- 使用项目的 XAML 文件可能需要更新(手动或使用适当的全局搜索和替换)
- 全部重建
- 犯罪!!(在移动之前提交更改)
Note: The folder in Windows Explorer remains the old name to this point (e.g., Utils.Foo). This is fixed in the move steps.
注意:Windows 资源管理器中的文件夹此时仍保留旧名称(例如,Utils.Foo)。这在移动步骤中是固定的。
Move
移动
This method: 1) retains git history, 2) leverages R# to adjust namespaces atomically and 3) updates dependent projects en masse (avoids tedious manual editing of dependent sln and csproj files).
这种方法:1) 保留 git 历史,2) 利用 R# 原子地调整命名空间和 3) 整体更新依赖项目(避免对依赖 sln 和 csproj 文件进行繁琐的手动编辑)。
- unload all the projects in the solution (so that removal of the target project does not trigger changes in dependent projects) - VS | select all solution folders under the Solution | right-click Unload Projects 
- move folders using git (so history is maintained) 
- 卸载解决方案中的所有项目(以便删除目标项目不会触发依赖项目的更改) - VS | 选择解决方案下的所有解决方案文件夹| 右键单击卸载项目 
- 使用 git 移动文件夹(因此历史被保留) 
a) open Developer Command Prompt for 2019
a) 打开 2019 年的开发人员命令提示符
b) git status (to illustrate “nothing to commit, working tree clean”)
b) git status(说明“什么都不提交,工作树干净”)
c) git mv the project e.g., git mv "C:\Code\foo\foo\Utils.Foo" "C:\Code\Foo"
c) git mv 项目,例如, git mv "C:\Code\foo\foo\Utils.Foo" "C:\Code\Foo"
d) git status to view/verify change
d) git status 查看/验证更改
- remove the project
- 删除项目
VS | Solution Explorer | select project | right-click | Remove (since all projects are unloaded, this will correctly NOT remove the references to it in dependent projects)
VS | 解决方案资源管理器 | 选择项目 | 右击 | 删除(因为所有项目都已卸载,这将不会正确删除相关项目中对其的引用)
- re-add the project (to the new location in the tree in Solution Explorer)
- 重新添加项目(到解决方案资源管理器树中的新位置)
a) VS | Solution Explorer | select target parent folder | right-click | Add | Existing Project
一)VS | 解决方案资源管理器 | 选择目标父文件夹 | 右击 | 添加 | 现有项目
- reload all projects
- 重新加载所有项目
IMPORTANT:Confirm that *.csproj files for dependent projects have been updated.
重要提示:确认依赖项目的 *.csproj 文件已更新。
(VS | Team Explorer | Changes | double-click any dependent csproj listed | inspect-verify ProjectReference path change)
(VS | 团队资源管理器 | 更改 | 双击列出的任何依赖 csproj | 检查-验证 ProjectReference 路径更改)
- Manually fix paths in the single moved *.csproj file
- 手动修复单个移动的 *.csproj 文件中的路径
Use Notepad++ (or other text editor) to fix the paths. Often this can be done with a simple search-and-replace (e.g., ../../../../ to ../../).
使用 Notepad++(或其他文本编辑器)修复路径。通常这可以通过简单的搜索和替换来完成(例如,../../../../ 到 ../../)。
This will update...
这将更新...
a) GlobalAssmeblyInfo.cs references
a) GlobalAssmeblyInfo.cs 参考
b) paths to packages
b) 包的路径
c) paths to Dependency Validation diagram files
c) 依赖验证图文件的路径
d) paths to ruleset paths (e.g., <CodeAnalysisRuleSet>..\..\..\..\SolutionDependencyValidation\IgnoreWarnings.ruleset</CodeAnalysisRuleSet>)
d) 规则集路径的路径(例如,<CodeAnalysisRuleSet>..\..\..\..\SolutionDependencyValidation\IgnoreWarnings.ruleset</CodeAnalysisRuleSet>)
- Close and re-Open the solution (to get the project references into good shape)
- 关闭并重新打开解决方案(以使项目引用保持良好状态)
Save All, Close Solution, I prefer to delete bin and obj folders to be clean of history, Re-open Solution
全部保存,关闭解决方案,我更喜欢删除 bin 和 obj 文件夹以清除历史记录,重新打开解决方案
- Validate
- 证实
a) VS | Team Explorer | Changes
一)VS | 团队资源管理器 | 变化
i) should see Staged Changes that reveal the files that moved ii) should see dependent projects (*.csproj) that were nicely updated review the csproj diffs and notice that the paths have been beautifully updated!! (this is the magic that avoids laboriously manually updating the csproj files using a text editor)
i) 应该看到显示移动的文件的阶段性更改 ii) 应该看到很好更新的依赖项目 (*.csproj) 查看 csproj 差异并注意路径已经很好地更新了!!(这是避免使用文本编辑器费力手动更新 csproj 文件的神奇之处)
b) in Windows Explorer, verify old location is empty
b) 在 Windows 资源管理器中,验证旧位置为空
c) Clean Solution, Rebuild Solution, Run unit tests, Launch apps in sln.
c) 清理解决方案、重建解决方案、运行单元测试、在 sln 中启动应用程序。
- Commit!!
- 犯罪!!
回答by BWS1986
in visual studio comunity 2019, i did what Victor David Francisco Enrique says, but needed only to delete the .vs invisbile folder
在 Visual Studio comunity 2019 中,我做了 Victor David Francisco Enrique 所说的,但只需要删除 .vs invisbile 文件夹
回答by Forch
It's easy in VS2012; just use the change mapping feature:
在 VS2012 中很容易;只需使用更改映射功能:
- Create the folder where you want the solution to be moved to.
- Check-in all your project files (if you want to keep you changes), or rollback any checked out files.
- Close the solution.
- Open the Source Control Explorer.
- Right-click the solution, and select "Advanced -> Remove Mapping..."
- Change the "Local Folder" value to the one you created in step #1.
- Select "Change".
- Open the solution by double-clicking it in the source control explorer.
- 创建要将解决方案移动到的文件夹。
- 签入所有项目文件(如果您想保留更改),或回滚任何签出的文件。
- 关闭解决方案。
- 打开源代码管理资源管理器。
- 右键单击解决方案,然后选择“高级 -> 删除映射...”
- 将“本地文件夹”值更改为您在步骤 1 中创建的值。
- 选择“更改”。
- 通过在源代码管理资源管理器中双击打开解决方案。
回答by markk
I figured out this try this it worked for me.
我想出了这个尝试这对我有用。
In visual studio 2017 community edition it creates a project at this path "C:\Users\mark\source\repos\mipmaps\mipmaps" This will create a access to file is denied issue
在 Visual Studio 2017 社区版中,它会在此路径“C:\Users\mark\source\repos\mipmaps\mipmaps”创建一个项目,这将创建对文件的访问被拒绝的问题
Now, you can fix that this way.
现在,您可以通过这种方式解决这个问题。
close your visual studio process. Then, find your project and copy the project folder But, first make a Sub-folder Named Projects inside of your visual studio 2017 folder in documents. Next, paste the project folder inside of your visual studio 2017 Project folder not the main visual studio 2017 folder it should go into the Sub-folder called Projects. Next, restart Visual studio 2017 Then, choose Open project Solution Then, find your project you pasted in your visual studio 2017 Projects folder Then clean the Project and rebuild it , It, should build and compile just fine. Hope, this Helped out anybody else. Not to sure why Microsoft thought building your projects in a path where it needs write permissions is beyond me.
关闭你的视觉工作室进程。然后,找到您的项目并复制项目文件夹但是,首先在文档中的 Visual Studio 2017 文件夹中创建一个名为 Projects 的子文件夹。接下来,将项目文件夹粘贴到您的 Visual Studio 2017 Project 文件夹中,而不是主 Visual Studio 2017 文件夹中,它应该进入名为 Projects 的子文件夹中。接下来,重新启动Visual Studio 2017 然后,选择打开项目解决方案然后,找到您粘贴到Visual Studio 2017 项目文件夹中的项目,然后清理项目并重建它,它应该可以正常构建和编译。希望,这帮助了其他人。不确定为什么 Microsoft 认为在需要写入权限的路径中构建您的项目超出了我的范围。
回答by Bob Lokerse
I wanted the changes in Git to be shown as moves/renames instead of delete & adds. So I did a combo of the above and this post.
我希望 Git 中的更改显示为移动/重命名而不是删除和添加。所以我做了上面和这篇文章的组合。
mkdir subdirectory
git mv -k ./* ./subdirectory
# check to make sure everything moved (see below)
git commit
And adjust the paths of the projects and of the assemblies from the nuget Pkg's in the sln file via a text editor.
并通过文本编辑器从 sln 文件中的 nuget Pkg 调整项目和程序集的路径。

