22

Is it possible to hide files/folders in .net core csproj without excluding them from build? I have a folder containing generated files which I would rather see they are not visible inside Solution Explorer in Visual Studio.

redman
  • 2,115
  • 5
  • 32
  • 59

2 Answers2

43

You can set the Visible="false" attribute on the items.

For an SDK-based project (.net core / asp.net core templates), you could add e.g.:

<ItemGroup>
  <Content Update="**/*.css" Visible="false" />
</ItemGroup>

Depending on your project type (=> defaults), you might have to replace Content with None for the particular type, or even Compile for generated code files.

Martin Ullrich
  • 94,744
  • 25
  • 252
  • 217
  • 3
    Is it possible to hide folder too? This hides all files in folder Generated, but the empty folder is still visible... – redman Aug 10 '17 at 12:26
  • 1
    To my knowledge, this is not possible. Many new tools try to autogenerate into `obj` (`$(BaseIntermediateOutputPath)`) and include the items during the build and not as static ("root level") items using the `Link` metadata... But yes I also have stray `dist` / `css` folders etc. – Martin Ullrich Aug 10 '17 at 12:30
  • I find that setting Visible to false means that VS will not trigger a build if one of these files changes. https://developercommunity.visualstudio.com/content/problem/140935/invisible-project-items-do-not-trigger-a-build.html – Tim Sparkles Oct 30 '17 at 20:34
  • 2
    Is this supposed to work with .Net Core projects? I added the `Visible` tag and the file still shows in solution explorer. – julealgon Jan 02 '19 at 22:21
  • 9
    For NetCore and NetStandard It would not work for me unless I used None. ` ` – Codie Morgan Jul 07 '19 at 19:09
  • @CodieMorgan there is a difference between hiding files and removing them from the project. ` – Martin Ullrich Jul 08 '19 at 08:46
  • 1
    By me it isn't available as attribute only as an element False – yoel halb Jul 16 '19 at 00:03
0

Thanks to Martin answer, I found a way to apply it to a whole folder using **\**. For example for an ASP.NET Core wwwroot\Scripts folder which contains files compiled by TypeScript (TypeScriptCompile):

    <ItemGroup>
        <TypeScriptCompile Update="wwwroot\Scripts\**\**" Visible="False" />
        <Content Update="wwwroot\Scripts\**\**" Visible="False" />
    </ItemGroup>

As his answer noted, you can add more type in there like None.

Luke Vo
  • 17,859
  • 21
  • 105
  • 181