Determine current operating system in C#
This post will discuss how to determine the operating system in C#.
1. Using RuntimeInformation Class
The recommended solution to identify the current platform is to use the RuntimeInformation.IsOSPlatform() method from System.Runtime.InteropServices namespace. It takes an argument of type OSPlatform Struct, which has four properties: Windows, Linux, OSX, and FreeBSD representing Windows, Linux, OSX, and FreeBSD operating systems respectively.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
using System; using System.Runtime.InteropServices; public class Example { public static void Main() { if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { Console.WriteLine("Windows"); } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { Console.WriteLine("Linux"); } else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { Console.WriteLine("MacOS"); } } } |
You can get more information about the current operating system using the RuntimeInformation.OSDescription property. For example,
|
1 2 3 4 5 6 7 8 9 10 |
using System; using System.Runtime.InteropServices; public class Example { public static void Main() { Console.WriteLine("OSDescription: {0}", RuntimeInformation.OSDescription); } } |
Sample Output:
OSDescription: Microsoft Windows 10.0.22000
or
OSDescription: Unix 4.15.0.176
2. Using OperatingSystem Class
The OperatingSystem class contains several static utility methods and properties that provide information about the current runtime operating system. To determine whether the current application is running on Windows, Linux, macOS, or Android, you can use IsWindows(), IsLinux(), IsMacOS(), IsAndroid() methods respectively. For example,
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
using System; public class Example { public static void Main() { if (OperatingSystem.IsWindows()) { Console.WriteLine("Windows"); } else if (OperatingSystem.IsLinux()) { Console.WriteLine("Linux"); } else if (OperatingSystem.IsMacOS()) { Console.WriteLine("MacOS"); } else if (OperatingSystem.IsAndroid()) { Console.WriteLine("Android"); } } } |
You can get more information about the current operating system like operating system platform and version with the Environment.OSVersion property.
|
1 2 3 4 5 6 7 8 9 10 |
using System; public class Example { public static void Main() { var os = Environment.OSVersion; Console.WriteLine("Platform: {0}, Version: {1}", os.Platform, os.VersionString); } } |
Sample Output:
Platform: Win32NT, Version: Microsoft Windows NT 10.0.22000.0
or
Platform: Unix, Version: Unix 4.15.0.176
3. Using Environment.OSVersion Property
In .NET 5 and later, you can get the current operating system name and version using the Environment.OSVersion property.
|
1 2 3 4 5 6 7 8 9 |
using System; public class Example { public static void Main() { Console.WriteLine(Environment.OSVersion); } } |
Sample Output:
Microsoft Windows NT 10.0.22000.0
or
Unix 4.15.0.176
Be careful while using this in .NET versions before .NET 5. If your application runs under Windows compatibility mode, Environment.OSVersion may result in an incorrect OS version.
That’s all about determining the operating system in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)