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

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

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


当前回答

在Visual Studio 2013中,当右键单击一个文件夹时,我无法让“包含在项目中”工作。所做的工作是展开文件夹,选择所有的文件,然后选择“包括在项目”。这是相当乏味的,因为你必须一个一个地执行每个文件夹(但至少你可以一次执行每个文件夹中的所有文件),而且它似乎存储了文件路径(你可以通过查看文件上的属性和查看“相对路径”选项来查看)。

我希望使用它在Visual Studio Installer项目中部署一些数据文件,它似乎会选择包含的文件并保存它们的路径。

其他回答

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

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

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

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

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

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

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

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

我找不到令我满意的答案,所以我自己琢磨。

如果你想向你的项目添加外部源代码,而不想复制整个代码,这里是你的答案。我有很多依赖于其他gits,他们每小时更新,如果不是一分钟。我不能每小时拷贝一次来同步。以下是你需要做的。

假设这是结构:

/root/projA/src

/root/projA/includes

/root/projB/src

/root/projB/include

/root/yourProj/src

/root/yourProj/include

Start your VS solution. Right-click the project name right below the Solution. Then click the "Add", "New Filter", put the name "projA" for projA. Right-click on the "projA", click "Add", "New Filter", enter name "src" Right-click on the "projA", click "Add", "New Filter", enter name "includes" Right-click "projA"/"src", click "Add", "Existing Item", then browse to the /root/projA/src to add all source codes or one by one for the ones you want. Do same for "projA"/"includes" Do same for projB. Now the external/existing projects outside yours are present in your solution/project. The VS will compile them together. Here is an trick. Since the projA and projB are virtual folders under your project, the compiler may not find the projA/includes. If it doesn't find the projA/includes, then right click the project, select the "Properties". Navigate to "C/C++". Edit "Additional Include Directories", add your projA/include as such "../projA/includes", relative path.

一个警告,如果有重复的包含/头文件,“头文件”上的“排除项目”实际上不起作用。这是VS中的一个bug。

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

下面是项目文件示例。

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

在Visual Studio 2013中,当右键单击一个文件夹时,我无法让“包含在项目中”工作。所做的工作是展开文件夹,选择所有的文件,然后选择“包括在项目”。这是相当乏味的,因为你必须一个一个地执行每个文件夹(但至少你可以一次执行每个文件夹中的所有文件),而且它似乎存储了文件路径(你可以通过查看文件上的属性和查看“相对路径”选项来查看)。

我希望使用它在Visual Studio Installer项目中部署一些数据文件,它似乎会选择包含的文件并保存它们的路径。