简体   繁体   中英

Visual studio 2019 not automatically creating folder structure from .csproj files

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 (eg <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.

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>

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:

在此处输入图像描述

And Windows Explorer shows:

在此处输入图像描述

This is the same behaviour in VS2019.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM