如何在Visual Studio中将现有目录树添加到项目?

时间:2023-01-13 09:54:30

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的文件夹上“添加现有文件”并导航到目录结构中的文件..服务>帐户管理> 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重新创建每个文件夹?

10 个解决方案

#1


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

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

如何在Visual Studio中将现有目录树添加到项目?

如何在Visual Studio中将现有目录树添加到项目?

#2


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

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

#3


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

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

If you wanted 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

#4


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.

    右键单击该文件夹上的Windows资源管理器,然后单击包含许多文件和文件夹的文件夹。

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

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

  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.

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

#5


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. 以管理员身份打开cmd提示符

  3. mklink /d [current project directory name] [directory in other project it should point to]
  4. 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. 使用上述步骤在项目文件夹中创建链接。

  3. In Visual Studio... select project in Solution Explorer.
  4. 在Visual Studio中...在解决方案资源管理器中选择项目

  5. At the top of Solution Explorer... click the Show All Files button (may need to click it twice if already active).
  6. 在解决方案资源管理器的顶部...单击“显示所有文件”按钮(如果已经激活,则可能需要单击两次)。

  7. The link will now show in your project... right-click and choose Include In Project.
  8. 该链接现在将显示在您的项目中...右键单击并选择“包含在项目中”。

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

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

#6


In Visual Studio 2017, you switch between Solution View and Folder View back 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文件等。

如何在Visual Studio中将现有目录树添加到项目?

#7


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 *.csproj file 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环境变量,您可以以相同的方式使用内置的Environment变量。

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

最后一个示例组是最终输出中需要的一堆内容文件。有关详细信息,请参阅https://*.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

#8


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 Installer项目中部署一些数据文件,它似乎拿起包含的文件并保留它们的路径。

#9


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”我想要的是在我的Code文件夹下递归包含代码。

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>

#10


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中执行此操作的唯一方法类似于拖放方法。右键单击要添加项目的解决方案。应用程序菜单将有一个add ...项目。打开它,您会发现其中一个选项是将现有项目添加到解决方案中。

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.

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

#1


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

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

如何在Visual Studio中将现有目录树添加到项目?

如何在Visual Studio中将现有目录树添加到项目?

#2


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

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

#3


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

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

If you wanted 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

#4


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.

    右键单击该文件夹上的Windows资源管理器,然后单击包含许多文件和文件夹的文件夹。

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

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

  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.

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

#5


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. 以管理员身份打开cmd提示符

  3. mklink /d [current project directory name] [directory in other project it should point to]
  4. 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. 使用上述步骤在项目文件夹中创建链接。

  3. In Visual Studio... select project in Solution Explorer.
  4. 在Visual Studio中...在解决方案资源管理器中选择项目

  5. At the top of Solution Explorer... click the Show All Files button (may need to click it twice if already active).
  6. 在解决方案资源管理器的顶部...单击“显示所有文件”按钮(如果已经激活,则可能需要单击两次)。

  7. The link will now show in your project... right-click and choose Include In Project.
  8. 该链接现在将显示在您的项目中...右键单击并选择“包含在项目中”。

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

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

#6


In Visual Studio 2017, you switch between Solution View and Folder View back 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文件等。

如何在Visual Studio中将现有目录树添加到项目?

#7


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 *.csproj file 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环境变量,您可以以相同的方式使用内置的Environment变量。

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

最后一个示例组是最终输出中需要的一堆内容文件。有关详细信息,请参阅https://*.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

#8


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 Installer项目中部署一些数据文件,它似乎拿起包含的文件并保留它们的路径。

#9


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”我想要的是在我的Code文件夹下递归包含代码。

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>

#10


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中执行此操作的唯一方法类似于拖放方法。右键单击要添加项目的解决方案。应用程序菜单将有一个add ...项目。打开它,您会发现其中一个选项是将现有项目添加到解决方案中。

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.

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