Tip: Visual Studio Custom File Nesting

Posted by

One of the niceties of Visual Studio is the ability to nest files under similarly named files. If I have appsettings.prod.json and appsettings.qa.json in the same folder as the appsettings.json, they will nest under appsettings.json. While it doesn’t recognize every file extension, you can create a custom file nesting json configuration, which is good because I have multiple .yml files that follow that same pattern.

If you look closely at the options in the solution explorer, there’s a diagonal line that is really an icon of nested boxes. After clicking this button’s dropdown menu, you can choose to “Add Custom Settings”.

After clicking this, you get an option to create a custom settings file that you can base off of the Web nesting settings. Within the dependentFIleProviders pathSegment, you can add a custom file extension, such as “.yml”.

Saving it and changing the current settings to the newly created custom settings applies almost immediately, and my .yml files are now nested.

This settings file is stored in the %USERPROFILE%\AppData\Local\Microsoft\VisualStudio\NestingProfiles folder, so it won’t be added into the direct source code. That is to say, this settings file is a Visual Studio configuration file, not a part of the code project. This is unfortunate because that settings can’t be easily shared via the source code, but is also very nice because those same nesting settings can be leveraged regardless of what project you have open.

I will admit this isn’t much of a seriously beneficial feature of Visual Studio, but it’s a pretty nice one for keeping your solution explorer easier to navigate.

2 comments

  1. I appreciate the attempt to share, but I couldn’t get this working on VS 2019 (16.3.2). The solution I’m working with ATM and that I tested with contains a mix of projects targeting .net 4.6.1 and some .net core 3.0, and some multitarget projects (with those two targets). Solution explorer doesn’t show the nesting options button, nor any of the nesting-related context menu items.

    Then I attempted “going Spanish” (perfectly normal expression in Norwegian; a reference to the general impression Norwegians have of how people drive in Spain, meaning taking shortcuts of dubious legality!) and just put a file in the “NestingProfiles” folder, but I don’t *think* VS is trying to use it (it didn’t have any effect, at least).

    Anyway, to get to the point: If you provided formatted text instead of using screenshots, it would be a lot easier to grab some code and modify it. Taking pictures of code is maybe not the optimal way to provide it to others. 🙂

    1. I checked the Visual Studio Docs website and managed to find a page related to file nesting: https://docs.microsoft.com/en-us/visualstudio/ide/file-nesting-solution-explorer?view=vs-2019. It specifies that

      The feature is currently only supported for ASP.NET Core projects.

      For me, I didn’t have the file nesting available on a .net core console app until I added an ASP.NET Core Web Application to the project. Interestingly, if I remove that project from the solution, the option also disappears. So, if your .net core 3.0 projects are class libraries, that would make sense as to why the nesting isn’t available, and isn’t working when forcing it with the file in the NestingProfiles folder.

      Edit: apparently, according to a comment on GitHub, you can add a chunk of code to the class library .csproj to force it to have dynamic file nesting: https://github.com/dotnet/project-system/issues/3242#issuecomment-453995460


      < ItemGroup >
      < ProjectCapability Include="DynamicDependentFile" />
      < ProjectCapability Include="DynamicFileNesting" />

      Also sounds like there’s an option to create a file .filenesting at your project/solution with those settings in it, and that can work too (which would allow you to store your custom file nesting settings in a Git repo, and share it with your team easily).

      Thanks for the feedback regarding using screenshots. I typically try to do a good job of including code examples when I can, using demos via Plunker or Codepen when applicable, but I didn’t think it necessary in this instance, as I didn’t create the template settings. Creating the new nesting profile allowed me to select “Web” as the template, which scaffolded most of the settings for me. I opted to use screenshots since my actual change was minimal, and I was more highlighting a feature in Visual Studio. I’ll keep the feedback in mind moving forward though.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.