Yesterday’s post, “Checking If the Operating System Is macOS in C# & .NET”, looked at a quicker, simpler API to check whether your code is running on macOS.

In today’s post, we will look at a similar problem: checking for Linux.

Unsurprisingly, there is a similar API that you can use: OperatingSystem.IsLinux()

void Main()
{
  Console.WriteLine(OperatingSystem.IsLinux());
}

This just checks whether the underlying operating system is Linux. For more granular details, there are other APIs that you can turn to.

TLDR

You can use OperatingSystem.IsLinux() to check if your code is running on a Linux operating system.