Open .net framework 4.5 project in VS 2022. Is there any workaround?

Is there anyway to open and work on .net framework 4.5 project in visual studio 2022.

May be problem is not with VS2022 but as .net framework 4.5 developer pack is not available any more .. my project can not be changed target version .. Is there any workaround?

2

  • Download Microsoft.NETFramework.ReferenceAssemblies.net45 from nuget.org
  • Open the package as zip (or rename the package file and change its extension from .nupkg to .zip then open it)
  • copy the files from build.NETFrameworkv4.5 to C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.5
  • Close Visual Studio and then reopen it

For more details: Building a project that target .NET Framework 4.5 in Visual Studio 2022

12

While all of the proposed, and even the accepted, solutions will work, none of them are the preferred approach.

The only thing you need to do is add a reference to the NuGet package as follows:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net40</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies.net40" Version="1.0.2">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
  </ItemGroup>
</Project>

Figure 1: C# project targeting .NET Framework 4.0 via reference assemblies

You can make this change directly in the *.csproj or via the Package Manager UI in Visual Studio. The MSBuild extensions in the package will take care of the rest of the magic. There are no scripts to write, files to copy, or permissions to grant. This will work without elevation, which would be required to write under %PROGRAMFILES%.

Setting PrivateAssets="All" will make the package reference design-time only. It will not be picked up or included as a transitive dependency if your target also builds a NuGet package.

Figure 2: Project targeting .NET Framework 4.0 in the Visual Studio 2022 Solution Explorer

Roslyn Issue 56161: Remove .NET 4.5 Targeting Pack Requirement further confirms that this how you use reference assemblies when a targeting pack is not installed. This approach is useful any time you don’t want to install a targeting pack or it’s otherwise unavailable – say on a build server without Visual Studio.

The Proof

Figure 3: Older .NET targeting packs not installed or available

I ran into the same issue after uninstalling Visual Studio 2019. The build worked as expected from Visual Studio and the CLI after adding this package reference.

Example

  1. Download the files from the .NET 4.0 Console using VS2022 Gist to any folder
  2. Open a developer prompt and navigate to the target folder
  3. Execute dotnet restore
  4. Open the project in Visual Studio 2022
  5. Build and run the project

Additional Information

  • Building from the command-line with dotnet or msbuild should be error and warning free
  • You do not have to have the target .NET Framework version installed to build it, but you do to run it; including unit tests
  • You may need to run Clean and/or manually delete bin, obj, and possibly .vs to clear out initial Visual Studio warnings
  • You may need to perform an explicit NuGet restore (ex: dotnet restore) to clear initial Visual Studio warnings
  • Visual Studio appears to require the target .NET Framework be installed
  • The example shown targets .NET Framework 4.0 with .NET Framework 4.8 installed
  • Targeting .NET Framework 2.0 or 3.5 compiled via command-line, but not Visual Studio, likely because I don’t have them installed
  • If Visual Studio is still giving you grief:
    • Add at least one supported target framework moniker (ex: net48)
    • Use Visual Studio Code (I prefer full VS, but it’s an option)

16

From the comments on the article linked from the answer about copying the files: “Install “Windows 8 SDK”. And it can just install “.Net Framework 4.5 Software Development Kit”. No other baggage.”

This option was the quickest for me, as I can run an elevated installer easier than switching to elevated to write to the program files folders.

5

Another way to install multiple older targeting packs is to install Visual Studio 2019.

Note: it will delete them again if you uninstall VS2019, so take a copy of the ones you want and paste them back afterwards.

3

Here is an automated solution. It follows the steps in this answer: /a/70109092/569302 When it copies the files a dialog may appear that you have to click to replace files that already exist

(Note: You may need to update the “version” "1.0.2" to whatever is latest if there’s a newer NuGet package)

Run await DevHelpers.Download();

using Microsoft.VisualBasic.FileIO;
using System;
using System.Collections.Generic;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace j.Dev
{
    public class DevHelpers
    {
        public static async Task Download()
        {
            var version = "1.0.2";
            var name = "Framework45-" + DateTimeToFileString(DateTime.Now);
            var fileName = $"{name}.zip";
            var url = $"https://www.nuget.org/api/v2/package/Microsoft.NETFramework.ReferenceAssemblies.net45/{version}";
            await DownloadFile(fileName, url);
            ZipFile.ExtractToDirectory(fileName, name);
            var from = Path.Join(name, @"build.NETFrameworkv4.5");
            var to = @"C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.5";
            FileSystem.CopyDirectory(from, to, UIOption.AllDialogs);
        }

        private static string DateTimeToFileString(DateTime d)
        {
            return d.ToString("yyyy-dd-M--HH-mm-ss");
        }

        private static async Task DownloadFile(string fileName, string url)
        {
            var uri = new Uri(url);
            HttpClient client = new HttpClient();
            var response = await client.GetAsync(uri);
            using (var fs = new FileStream(
                fileName,
                FileMode.CreateNew))
            {
                await response.Content.CopyToAsync(fs);
            }
        }
    }
}

In my case I needed to download 4.0 AND 4.5, so here is the code that downloads both 4.0 and 4.5:

using Microsoft.VisualBasic.FileIO;
using System.IO.Compression;

namespace j.Dev
{
    /// <summary>
    /// Example Usage: await DevHelpers.DownloadAndCopyFramework4_0And4_5();
    /// </summary>
    public class DevHelpers
    {
        public static async Task DownloadAndCopyFramework4_0And4_5()
        {
            await DownloadAndCopyFramework4_0();
            await DownloadAndCopyFramework4_5();
        }

        public static async Task DownloadAndCopyFramework4_5()
        {
            await DownloadAndCopyFrameworkGeneric("net45", "v4.5", "1.0.2");
        }

        public static async Task DownloadAndCopyFramework4_0()
        {
            await DownloadAndCopyFrameworkGeneric("net40", "v4.0", "1.0.2");
        }

        public static async Task DownloadAndCopyFrameworkGeneric(string netVersion, string folder, string nugetVersion)
        {
            var name = netVersion + "-" + DateTimeToFileString(DateTime.Now);
            var fileName = $"{name}.zip";
            var url = $"https://www.nuget.org/api/v2/package/Microsoft.NETFramework.ReferenceAssemblies.{netVersion}/{nugetVersion}";
            await DownloadFile(fileName, url);
            ZipFile.ExtractToDirectory(fileName, name);
            var from = Path.Join(name, @"build.NETFramework" + folder);
            var to = @"C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFramework" + folder;
            FileSystem.CopyDirectory(from, to, UIOption.AllDialogs);
        }

        private static string DateTimeToFileString(DateTime d)
        {
            return d.ToString("yyyy-dd-M--HH-mm-ss");
        }

        private static async Task DownloadFile(string fileName, string url)
        {
            var uri = new Uri(url);
            HttpClient client = new HttpClient();
            var response = await client.GetAsync(uri);
            using (var fs = new FileStream(
                fileName,
                FileMode.CreateNew))
            {
                await response.Content.CopyToAsync(fs);
            }
        }
    }
}

3

For .NET 4.0, this command will install it for you if you have the Visual Studio 2019 files cached:

msiexec.exe /i "%ALLUSERSPROFILE%MicrosoftVisualStudioPackagesMicrosoft.Net.4.TargetingPack,version=4.0.30319.1netfx_dtp.msi" EXTUI=1

For .NET 4.5, this might work, though I haven’t tried it:

msiexec.exe /i "%ALLUSERSPROFILE%MicrosoftVisualStudioPackagesMicrosoft.Net.4.5.2.TargetingPack,version=4.5.51651.1netfx_452mtpack.msi" EXTUI=1

3

I had this issue with a project targeting both .NET Framework 4.5.2 and .NET 6 in VS2022, Windows 11, and fixed it by downloading the developer pack directly from Microsoft. The accepted answer did not work for me.

I used the link suggested by the compiler error I got: https://aka.ms/msbuild/developerpacks, and found 4.5.2 in the “out of support versions” section. It’s in a collapsed pane so you have to click the “out of support” element.

Direct link: https://dotnet.microsoft.com/en-us/download/dotnet-framework/thank-you/net452-developer-pack-offline-installer

A auto script in powershell to install the .NET Framework in VS22 (assuming is installed in the default path)

$isAdmin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)

if (-not $isAdmin) {
    Start-Process powershell.exe -Verb RunAs -ArgumentList "-File `"$PSCommandPath`""
    exit
}

$versions = @("net40", "net45", "net451", "net452", "net46", "net461", "net462", "net47", "net471", "net472", "net48")

foreach ($version in $versions) {
    $url = "https://www.nuget.org/api/v2/package/Microsoft.NETFramework.ReferenceAssemblies.$version"
    $outputZip = Join-Path -Path $env:TEMP -ChildPath "Microsoft.NETFramework.ReferenceAssemblies.$version.zip"
    $outputFolder = "C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFramework"

    Invoke-WebRequest -Uri $url -OutFile $outputZip
    Expand-Archive -Path $outputZip -DestinationPath $outputFoldertmp -force
    xcopy "$outputFoldertmpbuild.NETFramework*" $outputFolder /E /H /Y
    Remove-Item $outputZip -Force
    Remove-Item $outputFoldertmp -Recurse -Force
}

The following is what I did yesterday when several of my projects did not find the target framework 4.5.
After I installed Visual Studio 2019 and in Individual components check the .Net Framework 4.5 section (in Visual Studio 2022 there is no this option).

Hope this helps.

Install Visual Studio 2019 > Individual Component > Check On Net Framework Target 4.5

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật