Search code examples
c#visual-studiowinformsdependenciesvisual-studio-2022

How to add reference to framework component in Visual Studio?


Using Visual Studio 2022, how do I add framework dependencies to a class library?

If I right click on Dependencies, I have options to add Project, Shared Projects, COM and Browse references. There is no option to add Framework dependencies.

Looking at my WinForms project, I see the component I want is C:\Program Files\dotnet\packs\Microsoft.WindowsDesktop.App.Ref\6.0.8. So I can click the Browse button and navigate to this folder, but the Browse function wants a filename.

If I right click on the Frameworks entry under Dependencies, I get no context menu whatsoever.

I see no way to add this component to the list of dependencies for this class library project.


Solution

  • One way to get this done is to modify your csproj file. You need to change the target framework and tell it to use WinForms:

    <Project Sdk="Microsoft.NET.Sdk"> 
    
      <PropertyGroup>
        ...
        <TargetFramework>net6.0-windows</TargetFramework> <!-- Change this -->
        <UseWindowsForms>true</UseWindowsForms> <!-- Add this -->
        ...
      </PropertyGroup>
    
    </Project>