visual-studio 如何将现有目录树添加到 Visual Studio 中的项目?

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

How do I add an existing directory tree to a project in Visual Studio?

visual-studio

提问by Brian Leahy

The issue is simple really. Instead of creating folders in Visual Studio, I create a directory structure for my project on the file system. How do I include all the folders and files in a project, keeping the structure?

问题其实很简单。我没有在 Visual Studio 中创建文件夹,而是在文件系统上为我的项目创建一个目录结构。如何在项目中包含所有文件夹和文件,同时保持结构?

If I "Add Existing File" on a folder named Services and navigate to a file in the directory structure .. Services > AccountManagement > CreateAccount.cs, it appears in Visual Studio like so: Services > CreateAccount.cs. I do not want this.

如果我在名为 Services 的文件夹上“添加现有文件”并导航到目录结构中的一个文件 .. Services > AccountManagement > CreateAccount.cs,它会在 Visual Studio 中显示如下:Services > CreateAccount.cs。我不想要这个。

I have an entire directory structure worked out already, as I am mimicking our client developers using the same structure for organization. How do I add all the folders and files to the project in Visual Studio? Or do I have to do what most Microsoft users do and "put up with it" and recreate each and every folder through Visual Studio?

我已经制定了一个完整的目录结构,因为我正在模仿我们的客户开发人员使用相同的组织结构。如何将所有文件夹和文件添加到 Visual Studio 中的项目?还是我必须做大多数 Microsoft 用户所做的事情并“忍受它”并通过 Visual Studio 重新创建每个文件夹?

回答by Gant

You need to put your directory structure in your project directory. And then click "Show All Files" icon in the top of Solution Explorer toolbox. After that, the added directory will be shown up. You will then need to select this directory, right click, and choose "Include in Project."

您需要将目录结构放在项目目录中。然后单击解决方案资源管理器工具箱顶部的“显示所有文件”图标。之后,将显示添加的目录。然后,您需要选择该目录,右键单击,然后选择“包含在项目中”。

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

回答by Todd Smith

You can also drag and drop the folder from Windows Exploreronto your Visual Studio solution window.

您还可以将文件夹从Windows 资源管理器拖放到 Visual Studio 解决方案窗口。

回答by Yuchen Zhong

In Visual Studio 2015, this is how you do it.

在 Visual Studio 2015 中,您就是这样做的。

If you want to automatically include all descendant files below a specific folder:

如果要自动包含特定文件夹下的所有后代文件:

<Content Include="Path\To\Folder\**" />

This can be restricted to include only files within the path specified:

这可以限制为仅包含指定路径中的文件:

<Content Include="Path\To\Folder\*.*" />

Or even only files with a specified extension:

或者甚至只有具有指定扩展名的文件:

<Content Include="Path\To\Folder\*.jpg" >

Reference: http://jamesrpatterson.com/blog/automatic-include-in-project-for-visual-studio

参考:http: //jamesrpatterson.com/blog/automatic-include-in-project-for-visual-studio

回答by Catto

Copy & Paste.

复制和粘贴

To Add a folder, all the sub-directories, and files we can also Copy and Paste. For example we can:

要添加文件夹、所有子目录和文件,我们也可以复制和粘贴。例如我们可以:

  1. Right click in Windows explorer on the folder, and Copy on the folder with many files and folders.

  2. Then in Visual Studio Solution explorer, right click on the destination folder and click paste.

  3. Optional add to TFS; Then in the top folder right click and check in to TFS to check in all sub-folders and files.

  1. 在 Windows 资源管理器中右键单击文件夹,然后在包含许多文件和文件夹的文件夹上复制。

  2. 然后在 Visual Studio 解决方案资源管理器中,右键单击目标文件夹并单击粘贴。

  3. 可选添加到 TFS;然后在顶部文件夹中右键单击并签入 TFS 以签入所有子文件夹和文件。

回答by Edyn

You can use a symbolic link. This makes modifying the file in one project modify it in the other (as it's actually the same file).

您可以使用符号链接。这使得在一个项目中修改文件会在另一个项目中修改它(因为它实际上是同一个文件)。

To do this:

去做这个:

  1. Open cmd prompt as administrator
  2. mklink /d [current project directory name] [directory in other project it should point to]
  1. 以管理员身份打开cmd提示符
  2. mklink /d [当前项目目录名称] [它应该指向的其他项目中的目录]

This has it's drawbacks and pitfalls, but I use it on occasion for duplicate libraries that need different names.

这有它的缺点和陷阱,但我有时将它用于需要不同名称的重复库。

Edit for Anoop: Steps to add to Visual Studio:

为 Anoop 编辑:添加到 Visual Studio 的步骤:

  1. Create link in the project folder using the steps above.
  2. In Visual Studio... select project in Solution Explorer.
  3. At the top of Solution Explorer... click the Show All Files button (may need to click it twice if already active).
  4. The link will now show in your project... right-click and choose Include In Project.
  1. 使用上述步骤在项目文件夹中创建链接。
  2. 在 Visual Studio 中...在解决方案资源管理器中选择项目。
  3. 在解决方案资源管理器的顶部...单击“显示所有文件”按钮(如果已经处于活动状态,可能需要单击它两次)。
  4. 该链接现在将显示在您的项目中...右键单击并选择包含在项目中。

These are the steps I follow and works for a couple different projects.

这些是我遵循的步骤,适用于几个不同的项目。

回答by Siavash Mortazavi

In Visual Studio 2017, you switch between Solution Viewand Folder Viewback and forth. I think this is a better option, because it will keep the solution cleaner. I use this to edit .gitignore, .md files, etc.

在 Visual Studio 2017 中,您可以在解决方案视图文件夹视图之间来回切换。我认为这是一个更好的选择,因为它会使解决方案更清洁。我用它来编辑 .gitignore、.md 文件等。

Solution View and Folder View

解决方案视图和文件夹视图

回答by CAD bloke

To expand on Yuchen's answer, you can include files and paths as a link. This is not the same thing as adding the existing items because it doesn't make an extra copy in your project's folder structure. It is useful if you want one canonical folder / file etc to be used in a lot of different places but you only want to maintain one version/copy of it.

要扩展Yuchen 的答案,您可以将文件和路径作为链接包含在内。这与添加现有项目不同,因为它不会在项目的文件夹结构中制作额外的副本。如果您希望在许多不同的地方使用一个规范文件夹/文件等,但您只想维护它的一个版本/副本,这将非常有用。

Here is an example of what you can add to a *.csprojfile to create the link

这是您可以添加到*.csproj文件以创建链接的示例

<Compile Include="$(Codez)\z.Libraries\Common\Strings\RegexExtensions.cs">
    <Link>Helpers\RegexExtensions.cs</Link>
</Compile>

<Compile Include="..\..\z.Libraries\MoreLINQ\MoreLinq\ExceptBy.cs">
    <Link>Helpers\ExceptBy.cs</Link>
</Compile>

<Content Include="C:\Codez\Libs\Folder\OtherFolder\**\*.*">
    <Link>%(RecursiveDir)%(Filename)%(Extension)</Link>
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>

$(Codez)is a Windows Environment variable I defined, you can use the built-in Environment variables in the same manner.

$(Codez)是我定义的Windows环境变量,你可以用同样的方式使用内置的环境变量。

The last example group is a bunch of content files I need in the final output. See https://stackoverflow.com/a/11808911/492and other answers & links there for more on that.

最后一个示例组是我在最终输出中需要的一堆内容文件。有关更多信息,请参阅https://stackoverflow.com/a/11808911/492和其他答案和链接。

More MSBuild info at https://msdn.microsoft.com/en-us/library/bb629388.aspx

更多 MSBuild 信息,访问https://msdn.microsoft.com/en-us/library/bb629388.aspx

回答by RJ Thompson

I think I found a way to do this with the Compile Include=".\Code***.cs" What I wanted is to include code recursively under my Code folder.

我想我找到了一种方法来使用 Compile Include=".\Code***.cs" 我想要的是在我的代码文件夹下递归地包含代码。

Here is the project file sample.

这是项目文件示例。

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="15.0" DefaultTargets="BuildTarget">
    <PropertyGroup>
        <OutputType>Library</OutputType>
    </PropertyGroup>
    <PropertyGroup>
        <StartupObject />
    </PropertyGroup>
    <PropertyGroup>
        <RootNamespace>Autogen</RootNamespace>
    </PropertyGroup>
    <ItemGroup>
        <Compile Remove="@(Compile)" />
        <Compile Include=".\Code\**\*.cs" />
    </ItemGroup>
    <Target Name="BuildTarget">
        <Message Text="Build selected" Importance="high"/>
    </Target>
</Project>

回答by Malvineous

In Visual Studio 2013, I couldn't get "Include in Project" to work when right-clicking on a folder. What did work is expanding the folder, selecting all the files then choosing "Include in Project". It was quite tedious as you have to do each folder one by one (but at least you can do all files in each folder in one go), and it appears to store the file path (you can see this by viewing properties on the file and looking at the "Relative Path" option.)

在 Visual Studio 2013 中,右键单击文件夹时无法使“包含在项目中”起作用。所做的工作是展开文件夹,选择所有文件,然后选择“包含在项目中”。这是相当繁琐的,因为你必须一个一个地处理每个文件夹(但至少你可以一次完成每个文件夹中的所有文件),并且它似乎存储了文件路径(你可以通过查看文件的属性来看到这一点并查看“相对路径”选项。)

I am hoping to use this to deploy some data files in a Visual Studio Installer project, and it seems to pick up the included files and preserve their paths.

我希望用它来在 Visual Studio 安装程序项目中部署一些数据文件,它似乎选择包含的文件并保留它们的路径。

回答by Fenton Smith

As far as I can tell, the only way to do this in VS2010 is akin to the drag and drop method. Right click the solution to which you want to add a project. The application menu will have an add ... item. Opening that, you find that one of the options is to add an existing project to the solution.

据我所知,在 VS2010 中执行此操作的唯一方法类似于拖放方法。右键单击要添加项目的解决方案。应用程序菜单将有一个添加...项目。打开它,您会发现其中一个选项是将现有项目添加到解决方案中。

In the dialog box that opens, navigate to the folder containing the project file for the solution and select it. VS will, as part of importing that project file, also import the entire directory and, I assume any subordinate directories which are part of that project.

在打开的对话框中,导航到包含解决方案项目文件的文件夹并选择它。作为导入该项目文件的一部分,VS 还将导入整个目录,我假设任何属于该项目的下级目录。

As this does require an existing project file, it won't be impossible to import a directory tree until that tree has been converted to a project.

由于这确实需要现有的项目文件,因此在将目录树转换为项目之前导入目录树并非不可能。