1

I'm using VS2022, .NET7.0.

I'm trying to implement a function that automatically connects the SerialPort using USB's vid and pid.

I found that it was easier to implement by refering to this article rather than this article.

But, I can't use the Windows namespace in Winform.

I tried the Nuget\Install-Package Windows using PM, but it failed.

I add the <TargetPlatformVersion>8.0</TargetPlatformVersion> in .csproj of project and looked at the References Manager, but it hasn't changed.

Tried reinstalling Windows SDK 10, same thing.

How to solve this problem?

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
isakgo_
  • 750
  • 4
  • 15
  • Try installing Microsoft.Windows.SDK.Contracts. https://stackoverflow.com/a/60584094/17413993 – radian Feb 02 '23 at 00:40

2 Answers2

1

As per @radian's comment, I solved it like this:

Opened NuGet Package Management and installed Microsoft.Windows.SDK.Contracts.

I can now add Windows namespace normally.


EDIT

When I do the above method, there is a problem that it does not compile.

To fix this, you need to act on this post or the accepted answer.

isakgo_
  • 750
  • 4
  • 15
1

For a .NET 7, you do not need the NugetPackage, you need to edit the properties of the Project, and set the Target OS to 10.0.17763.0 or above. You can also modify the project file like this:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net7.0-windows10.0.17763.0</TargetFramework>
    <Nullable>enable</Nullable>
    <UseWindowsForms>true</UseWindowsForms>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>

</Project> 
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • you're right. It actually doesn't compile and I've been looking for a solution for the past hour. – isakgo_ Feb 02 '23 at 01:55
  • 1
    The NuGet package is for .NET 4.8. – Reza Aghaei Feb 02 '23 at 01:56
  • 1
    .NET 5, 6, and 7 use the TargetFramework solution. .NET 4.X, uses the NuGet package. You can take a look at the example here [MediaCapture API in WPF or Windows Forms](https://stackoverflow.com/a/74949797/3110834) or download code from repository. – Reza Aghaei Feb 02 '23 at 02:02