Overview
This article explains how to obtain information about the Operating System (OS) type and version where your application is currently running. By using the standard System.Environment class, you can identify platforms such as Windows, Linux, and macOS, and check detailed version numbers (Major, Minor, Build, and Revision).
Specifications (Input/Output)
- Input: None (refers to system information of the execution environment).
- Output:
- Platform identifier (
PlatformIDenum). - OS version string (
string). - Breakdown of version numbers (Major, Minor, Build, Revision).
- Platform identifier (
Basic Usage
Access the Environment.OSVersion property to obtain an object containing the current OS information.
OperatingSystem os = Environment.OSVersion;
// Example: Unix 13.0.0 (macOS, etc.) or Microsoft Windows NT 10.0.19045.0
Console.WriteLine(os.VersionString);
Full Code
The following console application retrieves basic OS information and individual version components.
using System;
class Program
{
static void Main()
{
// Retrieve OS information
OperatingSystem osInfo = Environment.OSVersion;
Console.WriteLine("=== OS Information ===");
// 1. Platform identifier (Win32NT, Unix, MacOSX, etc.)
// Note: In .NET Core/.NET 5 and later, macOS and Linux are often identified as "Unix".
Console.WriteLine($"Platform : {osInfo.Platform}");
// 2. Full version string
Console.WriteLine($"VersionString: {osInfo.VersionString}");
// 3. Version number details
Version ver = osInfo.Version;
Console.WriteLine($"Major : {ver.Major}");
Console.WriteLine($"Minor : {ver.Minor}");
Console.WriteLine($"Build : {ver.Build}");
Console.WriteLine($"Revision : {ver.Revision}");
// 4. Service Pack information (may be empty)
if (!string.IsNullOrEmpty(osInfo.ServicePack))
{
Console.WriteLine($"ServicePack : {osInfo.ServicePack}");
}
}
}
Execution Result Example (Windows 10/11)
=== OS Information ===
Platform : Win32NT
VersionString: Microsoft Windows NT 10.0.19045.0
Major : 10
Minor : 0
Build : 19045
Revision : 0
Execution Result Example (Linux/WSL)
=== OS Information ===
Platform : Unix
VersionString: Unix 5.15.90.1
Major : 5
Minor : 15
Build : 90
Revision : 1
Customization Points
- Integration into Logs: Outputting this information when an application starts helps identify the user’s environment (e.g., Windows 10 vs. 11, Linux kernel version) when troubleshooting bugs.
- Branching Logic: You can use the
osInfo.Platformvalue to branch logic, such as choosing Windows-specific path separators or determining if specific commands can be executed.
Points of Caution
- Accurate Windows 10/11 Versions: In older .NET Framework applications, the
Versionproperty might return6.2(Windows 8) if the application manifest (app.manifest) does not explicitly declare compatibility, even if running on Windows 10 or 11. Proper manifest settings are required for accurate results. - Distinguishing Between Linux and macOS: Since .NET Core, both Linux and macOS generally return
PlatformID.Unix. To distinguish them strictly, use the methods in theOperatingSystemclass described in the “Application” section.
Application: Modern OS Detection in .NET 5 and Later
In .NET 5 and later, the System.OperatingSystem class includes convenient methods for platform detection. These are recommended as guard clauses before calling OS-specific features.
using System;
if (OperatingSystem.IsWindows())
{
Console.WriteLine("This is Windows. You can perform registry operations, etc.");
// You can also use OperatingSystem.IsWindowsVersionAtLeast(10, 0, 19041)
}
else if (OperatingSystem.IsLinux())
{
Console.WriteLine("This is Linux.");
}
else if (OperatingSystem.IsMacOS())
{
Console.WriteLine("This is macOS.");
}
else if (OperatingSystem.IsBrowser())
{
Console.WriteLine("This is running in a browser, such as Blazor WebAssembly.");
}
Summary
Environment.OSVersion is a basic property for easily obtaining OS information. However, in modern cross-platform C# development (Windows, Linux, macOS), platform identification can be broad (e.g., grouped under Unix). For functional branching based on the OS, the best practice is to use methods introduced in .NET 5, such as OperatingSystem.IsWindows().
