C# - 사용중인 Windows 운영체제 버전 확인하기 (OS 종류 확인)

 

 

운영 체제 버전 번호

Windows 11 10.0*
Windows 10 10.0*
Windows Server 2022 10.0*
Windows Server 2019 10.0*
Windows Server 2016 10.0*
Windows 8.1 6.3*
Windows Server 2012 R2 6.3*
Windows 8 6.2
Windows Server 2012 6.2
Windows 7 6.1
Windows Server 2008 R2 6.1
Windows Server 2008 6.0
Windows Vista 6.0
Windows Server 2003 R2 5.2
Windows Server 2003 5.2
Windows XP 64비트 버전 5.2
Windows XP 5.1
Windows 2000 5.0

운영 체제 버전 정보 확인 코드

  • 운영체제 정보를 가져오는 클래스 : Environment
  • 버전의 정보를 가져오는 속성 : OSVersion
OperatingSystem os = Environment.OSVersion;

// MessageBox.Show(os + "");

 

  • 사용중인 운영체제는 Windows 10 Pro인데 버전 정보를 확인해보니 Windows 8 이라고 반환되었다.
  • 이유가 뭘까?


 

윈도우 8.1 또는 윈도우 10에 대한 애플리케이션 설정

  • Windows 8.1 또는 Windows 10에 대해 매니페스트되지 않은 애플리케이션은 Windows 8 버전 값(6.2)를 반환
  • 그렇기 때문에 Windows 10 Pro를 사용함에도 위의 확인 메시지박스에서 6.2의 버전정보가 반환 된 것

애플리케이션 매니페스트 설정 방법

  • 솔루션 탐색기에서 프로젝트의 속성(Properties) 펼치기
  • app.manifest 열기

 

<!-- example.exe.manifest -->
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3">
    <assemblyIdentity
        type="win32"
        name="Contoso.ExampleApplication.ExampleBinary"
        version="1.2.3.4"
        processorArchitecture="x86"
    />
    <description>Contoso Example Application</description>
    <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
        <application>
            <!-- Windows 10 and Windows 11 -->
            <supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"/>
            <!-- Windows 8.1 -->
            <supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}"/>
            <!-- Windows 8 -->
            <supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}"/>
            <!-- Windows 7 -->
            <supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>
            <!-- Windows Vista -->
            <supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}"/> 
        </application>
    </compatibility>
    <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
        <security>
            <requestedPrivileges>
                <!--
                  UAC settings:
                  - app should run at same integrity level as calling process
                  - app does not need to manipulate windows belonging to
                    higher-integrity-level processes
                  -->
                <requestedExecutionLevel
                    level="asInvoker"
                    uiAccess="false"
                />   
            </requestedPrivileges>
        </security>
    </trustInfo>
</assembly>

 

  • <application> 부분에서 해당하는 주석 제거

 

  • 주석 제거된 코드


Windows 10 Pro OS에서 다시 버전 확인 코드 실행

OperatingSystem os = Environment.OSVersion;

// MessageBox.Show(os + "");

 

  • 정상적으로 버전 정보가 호출된 모습


OS 세부 버전 정보 확인

  • Environment.OSVersion.Version.Major : OS의 맨 앞의 버전 번호를 가져옴
  • Environment.OSVersion.Version.Minor : OS의 두번째 버전 번호를 가져옴
OperatingSystem os = Environment.OSVersion;
int majorVersion = Environment.OSVersion.Version.Major;
int minorVersion = Environment.OSVersion.Version.Minor;

// MessageBox.Show("OS Version : " + os + "\nMajor Version : " + majorVersion + "\nMinor Version : " + minorVersion);

 

  • 실행 화면


간단 응용) 운영체제별 코드 수행하기

  • 예시를 위해 간단히 메이저 버전만 이용하여 운영체제 확인
OperatingSystem os = Environment.OSVersion;
int majorVersion = Environment.OSVersion.Version.Major;
int minorVersion = Environment.OSVersion.Version.Minor;

if (majorVersion == 10)
{
    MessageBox.Show("윈도우 운영체제가 10이거나 11입니다.");
}

 

  • 실행 화면


참고

Microsoft - Learn - .NET - System - Environment Class

Microsoft - Win32 - Windows System Information