Can files be nested in VS2017 Solution Explorer for .NET Core (non-ASP.NET Core) projects?

ぃ、小莉子 提交于 2019-12-02 16:55:27

I have it working in one of my Microsoft.NET.Sdk-style projects using something similar to the following:

<ItemGroup>
  <Compile Update="LocalDateTest.*.cs">
    <DependentUpon>LocalDateTest.cs</DependentUpon>
  </Compile>
</ItemGroup>

The trick here is to use Update instead of Include. This is because the implicit items are coming from a props file that is imported before the main project. An additional Include won't affect files that are already included, but they can be modified using Update.

In Visual Studio 2019, I have a .NET Core 2.2 <Project Sdk="Microsoft.NET.Sdk"> project in which I wanted the nicely-nested appsettings.json / appsettings.Development.json files, just like they do automatically for <Project Sdk="Microsoft.NET.Sdk.Web"> projects.

Here's what I had to add to the .CSPROJ:

  <ItemGroup>
    <Content Include="appsettings.json">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
    <Content Include="appsettings.Development.json">
      <DependentUpon>appsettings.json</DependentUpon>
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
  </ItemGroup>

After that, I had to unload/reload the project for the change to take effect in Solution Explorer. Note that I also set these files to always be copied to the output directory.

If you using .netstandardx.x you can not use NestedIn . It's not working.

You can do that manually in your .csproj

<ItemGroup><Compile Include="BaseClass">ChildClass.cs</Compile></ItemGroup>

If you use the same prefix it will nest files automatically.

Example:

AsemblyInfo.cs 
AsemblyInfo.local.cs
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!