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

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

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


当前回答

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

这样做:

以管理员身份打开cmd提示符 Mklink /d[当前项目目录名][应该指向其他项目中的目录]

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

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

使用上述步骤在项目文件夹中创建链接。 在Visual Studio中…在解决方案资源管理器中选择项目。 在解决方案资源管理器的顶部…点击显示所有文件按钮(如果已经激活,可能需要点击两次)。 该链接现在将显示在您的项目中…右键单击并选择“包含在项目中”。

这些是我遵循的步骤,并为几个不同的项目工作。

其他回答

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

Visual Studio 2017及更新版本支持新的轻量级.csproj格式,该格式被称为“SDK格式”。这种格式的优点之一是,默认情况下,文件是通配符,而不是包含包含的文件和文件夹列表。因此,使用这种新格式,您的文件和文件夹-添加在资源管理器或命令行-将自动拾取!

SDK格式的.csproj文件目前适用于以下项目类型:

类库项目 控制台应用程序 ASP。NET Core web应用程序 .NET任何类型的核心项目

要使用新格式,请创建一个新的。net Core或。net Standard项目。因为即使在Visual Studio 2019中,完整的. net框架模板也没有更新,要创建. net类库,请选择. net标准库模板,然后编辑项目文件以针对您选择的框架版本(新风格的项目格式可以在Visual Studio中编辑-只需在解决方案资源管理器中右键单击项目并选择“编辑项目文件”)。例如:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net46</TargetFramework>
  </PropertyGroup>
</Project>

进一步阅读:

旧csproj到新csproj: Visual Studio 2017升级指南 揭开SDK项目的秘密 MSDN: .NET Core的csproj格式的补充

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

如果你想向你的项目添加外部源代码,而不想复制整个代码,这里是你的答案。我有很多依赖于其他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。

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

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

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

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

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

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

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

我想我找到了一种方法来做到这一点,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>