'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?

enter image description here enter image description here



Solution 1:[1]

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

Solution 2:[2]

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:

<ItemGroup>
 <PackageReference Include="microsoft.netframework.referenceassemblies.net45"
                   Version="1.0.2" PrivateAssets="All" />
</ItemGroup>

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.

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.

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.

Solution 3:[3]

Here is an automated solution. It follows the steps in this answer: https://stackoverflow.com/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\.NETFramework\v4.5\");
            var to = @"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.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 Assemblies\Microsoft\Framework\.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);
            }
        }
    }
}

Solution 4:[4]

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

msiexec.exe /i "%ALLUSERSPROFILE%\Microsoft\VisualStudio\Packages\Microsoft.Net.4.TargetingPack,version=4.0.30319.1\netfx_dtp.msi" EXTUI=1

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

msiexec.exe /i "%ALLUSERSPROFILE%\Microsoft\VisualStudio\Packages\Microsoft.Net.4.5.2.TargetingPack,version=4.5.51651.1\netfx_452mtpack.msi" EXTUI=1

Solution 5:[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.

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Husam Ebish
Solution 2 Chris Martinez
Solution 3
Solution 4 user541686
Solution 5