0

I have noticed a minor difference in behavior between Visual Studio 2017 and Visual Studio 2019. When I open a solution with VS 2017 it will automatically create folders that are mentioned in the project files (e.g. <Folder Include="Data\"> in a .csproj file). Visual Studio 2019 won't do that though. Is there a way to change this behavior in Visual Studio 2019 so that it too creates those folders if they don't exist?

EDIT: I first noticed this problem in combination with TFVC. When other people add empty folders to their solution it gets added to the .csproj file but when checking it in the empty folder is ignored. When I get that project from TFVC and open it with VS 2017 the folders are automatically created but not with VS 2019.

I would like these folders to be created since the person checking them in expected them to be there. TFVC can't work with empty folders, so it seemed good to me that VS 2017 solved the problem. Unfortunately VS 2019 seems to behave slightly differently.

To reproduce:

  1. Create a c# Class library (.NET framework) version 4.7.2 project in Visual Studio 2017 or 2019
  2. Close the solution and edit the .csproj file
  3. Add An ItemGroup with folders that don't exist. The end result might look like something like this:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>81fa1651-0a21-4ed6-8626-763141ab67cf</ProjectGuid>
    <OutputType>Library</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>TestDotNetFramework</RootNamespace>
    <AssemblyName>TestDotNetFramework</AssemblyName>
    <TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
    <Deterministic>true</Deterministic>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="System"/>
    
    <Reference Include="System.Core"/>
    <Reference Include="System.Xml.Linq"/>
    <Reference Include="System.Data.DataSetExtensions"/>
    
    
    <Reference Include="Microsoft.CSharp"/>
    
    <Reference Include="System.Data"/>
    
    <Reference Include="System.Net.Http"/>
    
    <Reference Include="System.Xml"/>
  </ItemGroup>
  <ItemGroup>
    <Compile Include="Class1.cs" />
    <Compile Include="Properties\AssemblyInfo.cs" />
  </ItemGroup>
  <!-- I added these manually -->
  <ItemGroup>
    <Folder Include="TestFolder\" />
    <Folder Include="TestFolder2\Testing\" />
  </ItemGroup>
  
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
 </Project>
  1. Save the .csproj file
  2. Open the solution with Visual Studio 2017. You will see that the folders you added in the ItemGroup are created automatically.
  3. Remove those folders and open the solution with Visual Studio 2019. The folders are not created automatically.
  • Can you explain why you want VS to create empty folders for you? – Drew Noakes Feb 04 '21 at 09:14
  • Hi, any update about this issue? – Mr Qian Feb 11 '21 at 06:25
  • @PerryQian-MSFT has reported it to Microsoft (see below), and they are investigating the issue. Might take a while before they do anything about it though (if they even decide to change it). – JuniorDeveloper Feb 11 '21 at 08:43
  • @JuniorDeveloper, yes. Both of us cannot handle the issue here further here and have to wait the team's response. So I suggest you could [mark my answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) since it is a real issue which let other members know it, also the workaround does help for you. Or you could solve the issue by my workaround during the waiting process. Or have to wait for the response. I will follow the ticket and the ticket is under consideratio so far. – Mr Qian Feb 15 '21 at 08:40

2 Answers2

2

I also faced the situation in my side. And it seems that only net framework projects on VS2017 could realize this. And net core projects, also, net framework on VS2019 cannot create the real folder by Folder Include from csproj file.

I cannot explain it so that I have reported it to our DC Forum. You can vote it or add any comments if I did not describe the issue in detail.

Since the ticket might take a long time, as a workaround, you could use these on the csproj file of VS2019:

<ItemGroup>
    <Folder Include="TestFolder"/>
</ItemGroup>
<Target Name="CreateAppDataFolder" BeforeTargets="PrepareForBuild">
    <MakeDir Directories="$(ProjectDir)TestFolder" />
</Target>
Mr Qian
  • 21,064
  • 1
  • 31
  • 41
0

I think you might be mistaken. In VS2017, if I create a new .NET Core Console Application project then edit the project file to contain:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.2</TargetFramework>
  </PropertyGroup>
  
  <ItemGroup>
    <Folder Include="NonExistent" />
  </ItemGroup>

</Project>

Then Solution Explorer shows:

enter image description here

And Windows Explorer shows:

enter image description here

This is the same behaviour in VS2019.

Drew Noakes
  • 300,895
  • 165
  • 679
  • 742