Can files be nested in VS2017 Solution Explorer for .NET Core (non-ASP.NET Core) projects?
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 want to wildcard it instead of hand-crafting each entry, adding these lines to your .csproj file means that anything like Foo.tests.cs automagically gets nested under Foo.cs
Tested and working in VS2019 with .NET Core 3.1.0
<ItemGroup>
<Compile Update="**\*.tests.cs">
<DependentUpon>$([System.String]::Copy(%(Filename)).Replace(".tests",".cs"))</DependentUpon>
</Compile>
</ItemGroup>