Skip to content

Instantly share code, notes, and snippets.

@Foadsf
Created April 17, 2025 12:35
Show Gist options
  • Save Foadsf/06a31b8f57673ec504a0e9200704e6bd to your computer and use it in GitHub Desktop.
Save Foadsf/06a31b8f57673ec504a0e9200704e6bd to your computer and use it in GitHub Desktop.

Fixing MSB4019 Error: Microsoft.CSharp.Core.targets Not Found After Winget Install of Visual Studio 2019

Problem Description

After installing Visual Studio 2019 Professional using the Windows Package Manager (winget install Microsoft.VisualStudio.2019.Professional), attempting to build a .NET Framework (e.g., v4.8) project using MSBuild or devenv.com fails with the error:

error MSB4019: The imported project "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\Roslyn\Microsoft.CSharp.Core.targets" was not found. Confirm that the expression in the Import declaration "..." is correct, and that the file exists on disk.

This occurs because the default winget installation might be minimal and may not include all components required for older .NET Framework desktop development, even if the base product is installed. The build system specifically looks for C# core build targets within the VS 2019 installation path, not just in the modern .NET SDK path (like C:\Program Files\dotnet\sdk\...).

Symptoms

Running MSBuild directly on a simple .NET Framework 4.8 project results in the MSB4019 error mentioned above. Commands like vs_installer.exe update report no updates available, and vs_installer.exe repair (especially in quiet mode) may not resolve the issue.

Diagnosis

A Minimal Working Example (MWE) consisting of a basic HelloWorld.cs and a corresponding .csproj file targeting .NETFramework,Version=v4.8, when built directly with MSBuild.exe from the VS 2019 installation path ("C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\MSBuild.exe"), reproduced the exact MSB4019 error. This confirmed the issue was with the VS installation itself, not the specific project or NuGet dependencies.

Solution: Add the Required Workload via CLI

The fix is to explicitly add the ".NET desktop development" workload to your Visual Studio 2019 installation using the Visual Studio Installer's command-line interface.

  1. Open cmd or powershell as Administrator.

  2. Run the following command:

    start "" /wait "C:\Program Files (x86)\Microsoft Visual Studio\Installer\vs_installer.exe" modify --installPath "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional" --add Microsoft.VisualStudio.Workload.ManagedDesktop --quiet --norestart
    • modify: Tells the installer to change the existing installation.
    • --installPath "...": Specifies the VS 2019 Professional installation directory.
    • --add Microsoft.VisualStudio.Workload.ManagedDesktop: Adds the ".NET desktop development" workload. This workload includes the necessary Roslyn compilers and MSBuild targets (Microsoft.CSharp.Core.targets) for .NET Framework projects.
    • --quiet: Runs without showing the installer GUI.
    • --norestart: Prevents an automatic reboot if one is deemed necessary by the installer.
    • start "" /wait: Ensures the command prompt waits for the installer process to complete.

Verification

  1. Check File Existence (Optional): After the modify command completes, you can verify the file now exists:

    dir "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\Roslyn\Microsoft.CSharp.Core.targets"
  2. Re-run MWE Build: Navigate back to the MWE directory (C:\dev\msbuild_test in the example) and run the MSBuild command again:

    "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\MSBuild.exe" HelloWorld.csproj /t:Build /p:Configuration=Release /p:Platform=AnyCPU /v:n

    The build should now succeed.

Key Takeaway

A standard winget install of Visual Studio 2019 Professional might require explicit modification using vs_installer.exe modify --add <WorkloadID> to install specific workloads like ".NET desktop development" needed for certain project types (.NET Framework). Simply running update or repair might not be sufficient if the core workload components were never installed initially.

Alternative (GUI)

You can also achieve the same result using the Visual Studio Installer GUI:

  1. Run "Visual Studio Installer".
  2. Find your VS 2019 Professional installation and click "Modify".
  3. Go to the "Workloads" tab.
  4. Check the box for ".NET desktop development".
  5. Click "Modify" in the bottom right to apply the changes.
using System;
public class HelloWorld
{
public static void Main(string[] args)
{
Console.WriteLine("Hello, MSBuild!");
}
}
<?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>{YOUR-GUID-HERE}</ProjectGuid> <!-- Replace with a unique GUID -->
<OutputType>Exe</OutputType>
<RootNamespace>HelloWorld</RootNamespace>
<AssemblyName>HelloWorld</AssemblyName>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion> <!-- Match original project -->
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
<LangVersion>latest</LangVersion> <!-- Optional: Specify C# language version -->
</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" />
</ItemGroup>
<ItemGroup>
<Compile Include="HelloWorld.cs" />
</ItemGroup>
<!-- This is the critical import that relies on Core targets -->
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment