In this article, we are going to find out how to determine the operating system of a computer on which our .NET Core application is running.
Without further ado, let’s dive in!
Why Do We Need to Determine the Operating System?
In the past, the .NET framework limited programs to the Windows platform. However, with the evolution of .NET, it has become cross-platform. That means that developers can write programs in C# and run them not only on Windows machines, but also on Linux, macOS, and even mobile devices.
This change provided many new possibilities, but also nuances that we need to be careful about. As an example, operating systems have different conventions for file system paths. While Windows uses backlash as a separator, Linux and macOS use a forward slash. When we use paths, we should keep this in mind to ensure correct operation on all systems.
How to Determine the Operating System (Before .NET 5.0)?
To check what operating system our program is currently running on, we can use the RuntimeInformation
class. In it, we will find the IsOSPlatform()
method, which returns true
if the current system is the one passed as a parameter, and false
otherwise:
bool isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
We need to provide an instance of the OSPlatform
struct with the operating system we want to check for. There are three default static properties ready to use: Windows
, Linux
, and OSX
.
Create()
method on OSPlatform
and pass the newly created object as a parameter to the IsOSPlatform()
method:OSPlatform myOwnOperatingSystem = OSPlatform.Create("MyOwnOperatingSystem"); bool isMyOwnOperatingSystem = RuntimeInformation.IsOSPlatform(myOwnOperatingSystem);
An Example Use-Case
Now that we know how to use the IsOSPlatform()
method, we can create the path appropriately, depending on the operating system. Before we do this, let’s create a helper class in which we will include the IsOSPlatform()
callers with the appropriate arguments:
public class OsPlatformResolver : IOsPlatformResolver { public bool IsWindows() => RuntimeInformation.IsOSPlatform(OSPlatform.Windows); public bool IsLinux() => RuntimeInformation.IsOSPlatform(OSPlatform.Linux); public bool IsOsx() => RuntimeInformation.IsOSPlatform(OSPlatform.OSX); public bool IsFreeBsd() => RuntimeInformation.IsOSPlatform(OSPlatform.FreeBSD); public bool IsPlatform(OSPlatform osPlatform) => RuntimeInformation.IsOSPlatform(osPlatform); }
This step is not obligatory, but it will make the code more readable and easier to test.
Now we can implement a BasePathResolver
class that returns the appropriate path based on the operating system:
public class BasePathResolver { private readonly IOsPlatformResolver _osPlatformResolver; public BasePathResolver(IOsPlatformResolver osPlatformResolver) { _osPlatformResolver = osPlatformResolver; } public string GetBasePath(OSPlatform? otherPlatform = null) { var basePath = string.Empty; if (_osPlatformResolver.IsWindows()) { basePath = "C:\\MyApp\\"; } else if (_osPlatformResolver.IsLinux() || _osPlatformResolver.IsOsx()) { basePath = "/MyApp/"; } else if (_osPlatformResolver.IsFreeBsd() || (otherPlatform.HasValue && _osPlatformResolver.IsPlatform(otherPlatform.Value))) { throw new NotSupportedException(); } return basePath; } }
Determining Operating Systems in New Projects
The approach described above applies to any C# project, with one caveat. The static OSPlatform.FreeBSD
was not added until .NET Core 3.0 and so is only available when targeting .NET Core 3.0 and above. However, since .NET 5, we have a new technique available using the new static methods of the OperatingSystem
class:
bool isWindows = OperatingSystem.IsWindows(); bool isMacOs = OperatingSystem.IsMacOS(); bool isLinux = OperatingSystem.IsLinux();
We can also easily check other operating systems, including mobile ones. The class has many more methods, which allow us to check whether the program is called from a browser (IsBrowser()
), from a phone (IsIOS()
, IsAndroid()
), from an Apple watch (IsWatchOS()
) or an Apple TV (IsTvOS()
).