OS information via Runtime.InteropServices

Levering Runtime.InteropServices to execute OS specific functionality

Home DailyDrop

Daily Knowledge Drop

The Runtime.InteropServices namespace contains the RuntimeInformation static class which can be used to obtain a variety of information related to the operating system. This can be used to only execute certain code depending on the operating system on which the application is running.


Basic Information

The basic operating system information is all available on the static RuntimeInformation class and can be accessed via public properties:

Console.WriteLine($"Framework description: {RuntimeInformation.FrameworkDescription}");
Console.WriteLine($"OS architecture: {RuntimeInformation.OSArchitecture}");
Console.WriteLine($"OS description: {RuntimeInformation.OSDescription}");
Console.WriteLine($"Process architecture: {RuntimeInformation.ProcessArchitecture}");
Console.WriteLine($"Runtime identifier: {RuntimeInformation.RuntimeIdentifier}");

Executing the code in Windows:

Framework description: .NET 6.0.5
OS architecture: X64
OS description: Microsoft Windows 10.0.22000
Process architecture: X64
Runtime identifier: win10-x64

Executing the code on WSL (Windows Subsystem for Linux):

Framework description: .NET 6.0.4
OS architecture: X64
OS description: Linux 5.10.16.3-microsoft-standard-WSL2 #1 SMP Fri Apr 2 22:23:49 UTC 2021
Process architecture: X64
Runtime identifier: ubuntu.20.04-x64

Operating System

The namespace also contains a OSPlatform struct, which contains a list of common operating systems, and in conjunction with the IsOSPlatform method on RuntimeInformation, can be used to check the current operating system:

Console.WriteLine($"Is Windows?: {RuntimeInformation.IsOSPlatform(OSPlatform.Windows)}");
Console.WriteLine($"Is Linux?: {RuntimeInformation.IsOSPlatform(OSPlatform.Linux)}");

Executing the code in Windows:

Is Windows?: True
Is Linux?: False

Executing the code on WSL (Windows Subsystem for Linux):

Is Windows?: False
Is Linux?: True

This can also be used to executing specific code only when running on a certain operating system. Suppose we need to write to the Windows Event Log - this can only happen in Windows (there are better ways of handling logging, but for demo purposes, this is the requirement):

if(RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
    Console.WriteLine("Performing some Windows specific stuff - maybe writing to EventLog for example");
}

Simple and easy to use.


Notes

A useful namespace and class to be aware of, especially if doing cross platform development, or if just required to gather information about the operating system.


References

Khalid Abuhakmeh Tweet


Daily Drop 136: 11-08-2022

At the start of 2022 I set myself the goal of learning one new coding related piece of knowledge a day.
It could be anything - some.NET / C# functionality I wasn't aware of, a design practice, a cool new coding technique, or just something I find interesting. It could be something I knew at one point but had forgotten, or something completely new, which I may or may never actually use.

The Daily Drop is a record of these pieces of knowledge - writing about and summarizing them helps re-enforce the information for myself, as well as potentially helps others learn something new as well.
c# .net interop os