21

I'm just starting to learn C# following Brackeys on Youtube. Upon writing along I get this problem pop up in vscode:

{
"resource": "/d:/OneDrive/Programming/Youtube/brackeys/How To Program In C#/Basics/Program.cs",
"owner": "msCompile",
"code": "CA1416",
"severity": 4,
"message": "This call site is reachable on all platforms. 'Console.WindowHeight.set' is only supported on: 'windows'. [D:\\OneDrive\\Programming\\Youtube\\brackeys\\How To Program In C#\\Basics\\Basics.csproj]",
"startLineNumber": 11,
"startColumn": 13,
"endLineNumber": 11,
"endColumn": 13
}

I have found this Microsoft article talking about this Warning, but I do not understand the solution if it's actually that :(...

I have a simple program, just learning about Console class changing the terminal height and font color etc:

    using System;
    
    namespace Basics
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.Title = "Skynet";
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WindowHeight = 40;
    
                Console.WriteLine();
    
                Console.ReadKey();
    
            }
        }
    }

Does anyone have an idea on how to tackle this problem?

Jayzor
  • 353
  • 1
  • 2
  • 9
  • Here is a [near-identical question](https://stackoverflow.com/q/67117053/1688738) with a [more comprehensive answer](https://stackoverflow.com/a/67118963/1688738) than you have received here. – Hugh W Oct 29 '22 at 18:54
  • Does this answer your question? [CA1416. How to tell builder that only platform is Windows?](https://stackoverflow.com/questions/67117053/ca1416-how-to-tell-builder-that-only-platform-is-windows) – malat Jul 25 '23 at 08:07

3 Answers3

41

So the error is about this line:

Console.WindowHeight = 40;

You try to set the Window Height, which is a method decorated with the [SupportedOSPlatform("windows")] attribute.

In order to tell the application to only execute this line when in Windows wrap the method.

if (OperatingSystem.IsWindows())
{
  Console.WindowHeight = 40;
}

The compiler will recognize this and stop throwing the remark.

Kevin Verstraete
  • 1,363
  • 2
  • 13
  • 13
  • 1
    I use `PrincipalContext` (**DirectoryServices**) and I have many warnings about CA1416 in VS2022, in all my class, with several methods. – Kiquenet Jul 13 '22 at 06:38
  • You have other options too. See https://stackoverflow.com/a/67118963/1688738. – Hugh W Oct 29 '22 at 18:54
20

If you know you're only developing for Windows, you can mark your code as such:

using System.Runtime.Versioning;

[SupportedOSPlatform("windows")]
class Program
{
    ...
}

The advantage of this approach is you avoid peppering your code with IF statements to check the OS version.

You can also mark specific methods this way if you don't want it to apply to the whole class.

Or the warning might mention a specific OS version, like this:

'Program' is only supported on: 'Windows' 7.0 and later.

In that case, you could specify the version:

[SupportedOSPlatform("windows7.0")]

Also see this question.

Tawab Wakil
  • 1,737
  • 18
  • 33
  • 3
    Change ***Target OS*** in `properties for project` (Console, REST API,...) ? – Kiquenet Jul 13 '22 at 06:40
  • @Kiquenet Not sure why, but I found that the `Target OS` approach works for some projects but creates build errors in others. Seems like a good solution otherwise. – Tawab Wakil Aug 02 '22 at 15:54
4

Specify the following lines in your project file:

    <PropertyGroup>
        <TargetFramework>net7.0-windows</TargetFramework>
    </PropertyGroup>
Yuriy Gavrishov
  • 4,341
  • 2
  • 17
  • 29
  • this does not fix the warnings, at least not using "net6.0-windows" – flawesome Jan 24 '23 at 08:55
  • 1
    [Here](https://github.com/dotnet/roslyn-analyzers/issues/4983#issuecomment-852738069) is one reason why this might not fix the warnings. In short, either AssemblyInfo must be generated by the compiler, or one must manually add the `SupportedOSPlatform` attribute . – Roman Starkov Feb 21 '23 at 23:46