简体   繁体   English

如何制作 Visual Studio 2019 项目的副本?

[英]How to make copy of Visual Studio 2019 project?

I have a Visual Studio 2019 project.我有一个 Visual Studio 2019 项目。 Was asked to make trial changes.被要求进行试验更改。 Do not want to alter current project.不想改变当前项目。 Is there any way to copy a project so I do not have to start over from scratch?有什么方法可以复制项目,这样我就不必从头开始了吗?

Searched here and other forums and documentation and cannot find solution.在此处和其他论坛和文档中搜索并找不到解决方案。

Short answer: you can very simply copy the directory in which your project reside on disk.简短回答:您可以非常简单地复制您的项目在磁盘上的目录。 Open the new directory and double click on the .csproj file and you are now working in your new copy of the project.打开新目录并双击 .csproj 文件,您现在正在处理项目的新副本。 But, if you want to do that in a more professional and maintainable way, please read on...但是,如果您想以更专业和可维护的方式做到这一点,请继续阅读...


The best way to do that in a more robust way is using Git.以更健壮的方式做到这一点的最佳方法是使用 Git。

In the bottom right corner of Visual Studio find the "Add to Source Control" command, click it, then choose Git.在 Visual Studio 的右下角找到“添加到源代码管理”命令,单击它,然后选择 Git。 Now the command says "master", that is your main "copy" of the project: in Git, this is called your main "branch".现在命令显示“master”,这是您项目的主要“副本”:在 Git 中,这称为您的主要“分支”。

Now click on it, choose "New branch", give it a name (eg: trial), leave the checkbox "Checkout branch" checked, then click on "Create branch".现在点击它,选择“New branch”,给它一个名字(例如:trial),选中“Checkout branch”复选框,然后点击“Create branch”。 Now the command in the right bottom has changed to "trial".现在右下角的命令已更改为“试用”。

Now you have another copy of your project: you can work on it, save your work on local disk.现在您有了项目的另一个副本:您可以处理它,将您的工作保存在本地磁盘上。 When you are ready to save it in Git, press the pencil icon, that is located in the bottom toolbar, on the right, near the command you used before (the number near it shows how many files you have changed): click it, enter a "commit message", then click on "Commit all".当你准备好将它保存在 Git 中时,按下位于底部工具栏右侧的铅笔图标,靠近您之前使用的命令(它附近的数字显示您更改了多少文件):单击它,输入“提交消息”,然后单击“全部提交”。 Now you have saved all your work in the trial "branch" of your project in Git.现在,您已将所有工作保存在 Git 项目的试用“分支”中。

If you want to come back to your initial copy of the project, click on "trial", then choose "master", and voila... your are now working on the old copy or your project.如果您想回到项目的初始副本,请单击“试用”,然后选择“主”,瞧……您现在正在处理旧副本或您的项目。 You can switch between the two branches anytime, only remember that, before switching, you have to "commit" your changes, that is, save them in Git, as I explained above.您可以随时在两个分支之间切换,只需要记住,在切换之前,您必须“提交”您的更改,即,将它们保存在 Git 中,正如我上面解释的那样。

At some point, you may want to bring the changes you made in trial to master.在某些时候,您可能希望将您在试用中所做的更改带入掌握。 To do so, when you are working in master, click on master (always in the bottom toolbar, on the right), then choose "Manage branches", then right click on "trial", click on "Merge from...", then click the Merge button: you have now brought the changes made on trial into master.为此,当您在 master 中工作时,单击 master(始终在底部工具栏的右侧),然后选择“管理分支”,然后右键单击“试用”,单击“从...合并” ,然后单击“合并”按钮:您现在已将试用时所做的更改带入 master。 The only problem can arise if you have changed a file both in master and in trial, and the changes are conflicting: in this case, VS will warn you, show you the conflicting changes, and you will have to choose which version of the conflicting changes you want.如果您在 master 和 trial 中都更改了文件,并且更改发生冲突,则会出现唯一的问题:在这种情况下,VS 会警告您,向您显示冲突的更改,您必须选择冲突的版本你想要的改变。

If one don't want or can't use git, one can also copy and paste the project folder.如果不想或不能使用git,也可以复制并粘贴项目文件夹。 Once in the new folder, Here is what I usualy do (I assumed the old project is named " projectOld " and new one " projectNew ").进入新文件夹后,这是我通常所做的(我假设旧项目名为“ projectOld ”而新项目名为“ projectNew ”)。 :

  • rename projectOld.sln to projectNew.slnprojectOld.sln重命名为 projectNew.sln
  • open projectNew.sln and change the following line in order to set the new project name :打开projectNew.sln并更改以下行以设置新项目名称:
Project("{C9CE8BB8-8B4A-11D0-5E31-C9100A0BC942}") = "projectNew", "projectNew.vcxproj", "{66F6A241-22E1-4941-6DA3-B6D9E7A8AF86}"
  • rename projectOld.vcxproj to projectNew.vcxprojprojectOld.vcxproj重命名为projectNew.vcxproj
  • open projectNew.vcxproj and change the lines with the <!-- here --> comments打开projectNew.vcxproj并使用<!-- here -->注释更改行
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  [...]
  <PropertyGroup Label="Globals">
    [...]
    <RootNamespace>projectNew</RootNamespace>                 <!-- here -->
    [...]
    <ProjectName>projectNew</ProjectName>             <!-- here -->
  [...]
  <ItemGroup>     
    <ClCompile Include="..\..\..\..\..\IncludePath(1)\ImplementatFile(1).cpp" />  <!-- here -->
    [... up to last]
    <ClCompile Include="..\..\..\..\..\IncludePath(n)\ImplementatFile(n).cpp" />
    [...]
  <ItemGroup>
    [same thing for every ItemGroups]                 <!-- here -->
  [...]
  • rename projectOld.vcxproj.filters to projectNew.vcxproj.filtersprojectOld.vcxproj.filters重命名为projectNew.vcxproj.filters

  • rename projectOld.vcxproj.user to projectNew.vcxproj.userprojectOld.vcxproj.user重命名为projectNew.vcxproj.user

  • one can delete the Debug and X64 folders, they will be created again by mvsc.可以删除Debug和X64文件夹,mvsc会重新创建。

I suggest you go with Project Template inside Visual Studio.我建议你在 Visual Studio 中使用项目模板。 You create a project template, store it somewhere, then create a new project by using the stored template.您创建一个项目模板,将其存储在某处,然后使用存储的模板创建一个新项目。 Here are more details这里有更多细节

https://docs.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2010/ms247121(v=vs.100)?redirectedfrom=MSDN https://docs.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2010/ms247121(v=vs.100)?redirectedfrom=MSDN

Yes, with GIT branching you can create variations of a project easily.是的,通过 GIT 分支,您可以轻松地创建项目的变体。 But I think that was not the core of the question asked, because if you create a branch, it is kept separate.但我认为这不是问题的核心,因为如果你创建一个分支,它是分开的。

I had a similar scenario where I needed a similar project in the same solution (code first database).我有一个类似的场景,我需要在同一个解决方案(代码优先数据库)中有一个类似的项目。 A clone of the database project to start with and make some changes for a different database.数据库项目的克隆,以开始并为不同的数据库进行一些更改。 So in the end I wanted to have 2 databases, not identical but similar, in the same solution.所以最后我想在同一个解决方案中有 2 个数据库,不相同但相似。

Adding a new project in the solution and then copying the .CS-Files is possible but cumbersome because of the references and dependencies.在解决方案中添加一个新项目,然后复制 .CS-Files 是可能的,但由于引用和依赖关系很麻烦。

So what I did is:所以我所做的是:

  1. Close Visual Studio关闭 Visual Studio
  2. Open the file explorer and copy the entire folder (eg MyProjectA), rename the copy (eg to MyProjectB)打开文件资源管理器并复制整个文件夹(例如 MyProjectA),重命名副本(例如到 MyProjectB)
  3. Rename the .csproj project file of the copied project MyProjectB.将复制的项目 MyProjectB 的 .csproj 项目文件重命名。 Then open it with a text editor of your choice, to see if you need to adapt anything (for example, project references if you moved it to a different directory level; they usually refer to a different project with a relative path, eg <ProjectReference Include="..\\SomePathOfADifferentProject\\SomeProject.csproj"/> )然后使用您选择的文本编辑器打开它,看看您是否需要调整任何内容(例如,如果将其移动到不同的目录级别,则项目引用;它们通常引用具有相对路径的不同项目,例如<ProjectReference Include="..\\SomePathOfADifferentProject\\SomeProject.csproj"/> )
  4. Open Visual Studio, load the solution打开 Visual Studio,加载解决方案
  5. In the solution explorer, right click on the solution, select "Add existing project...".在解决方案资源管理器中,右键单击解决方案,选择“添加现有项目...”。 Now browse for MyProjectB to add it as existing project现在浏览 MyProjectB 以将其添加为现有项目

and you're done!你就完成了!

If you're using source control like GIT, make sure to check if the clone was correctly added (ie check if the files are tracked by source control).如果您使用像 GIT 这样的源代码管理,请确保检查是否正确添加了克隆(即检查文件是否由源代码管理跟踪)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Visual Studio 2017 和 Visual Studio 2019 之间为每个项目引用一份 vcxproj.filters 和 vcxproj.user - How to reference one copy of vcxproj.filters and vcxproj.user per project between visual studio 2017 and visual studio 2019 如何使用命令行在 Visual Studio 2019 中打开项目? - How to open a project in Visual Studio 2019 using command line? 如何在 Visual Studio 2019 上恢复正常的“新项目”窗口 - How to restore normal "New Project" windows on Visual Studio 2019 如何在 Visual Studio 2019 中安装 Biztalk 2020 项目模板? - How to install Biztalk 2020 Project Templates in Visual Studio 2019? 如何在 Visual Studio 2019 中创建 C 项目? - How do I create a C project in visual Studio 2019? 如何解决:Nuget Visual Studio 2019 消息 Project 'Default' not found - How to solve: Nuget Visual Studio 2019 message Project 'Default' not found 如何在 Visual Studio 2019 中打开 2017 Cordova 项目 - How to open a 2017 Cordova project in Visual Studio 2019 如何在 .NET Core 项目的 Visual Studio 2019 中添加 Crystal Report? - How to add Crystal Report in Visual Studio 2019 in .NET Core project? 如何将属性选项卡添加到 Visual Studio 2019 中的 class 库项目? - How to add the properties tab to a class library project in Visual Studio 2019? 如何在 Visual Studio 2019 中启动 Blazor Webassembly 项目 - How to start a Blazor Webassembly project in Visual Studio 2019
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM