src/vs/base/node/windowsVersion.ts
113 LOC · 53 covered · 60 uncovered · 8 ranges · 2262 concepts · 2 introducers · 1063 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
/*---------------------------------------------------------------------------------------------
windowsVersion.ts ×6
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as os from 'os';
import { isWindows } from '../common/platform.js';
let versionInfo: { release: string; buildNumber: number } | undefined;
/**
* Initializes the Windows version cache by reading from the registry.
*
* On Windows 8.1+, the `os.release()` function may return incorrect version numbers
* due to the deprecated GetVersionEx API returning compatibility-shimmed values
* when the application doesn't have a proper manifest. Reading from the registry
* gives us the real version.
*
* See: https://github.com/microsoft/vscode/issues/197444
*/
export async function initWindowsVersionInfo() {
if (versionInfo) {
return;
}
if (!isWindows) {
versionInfo = { release: os.release(), buildNumber: 0 };
return;
}
let buildNumber: number | undefined;
let release: string | undefined;
try {
const Registry = await import('@vscode/windows-registry');
const versionKey = 'SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion';
const build = Registry.GetStringRegKey('HKEY_LOCAL_MACHINE', versionKey, 'CurrentBuild');
if (build !== undefined) {
buildNumber = parseInt(build, 10);
if (isNaN(buildNumber)) {
buildNumber = undefined;
}
}
const major = Registry.GetDWORDRegKey('HKEY_LOCAL_MACHINE', versionKey, 'CurrentMajorVersionNumber');
const minor = Registry.GetDWORDRegKey('HKEY_LOCAL_MACHINE', versionKey, 'CurrentMinorVersionNumber');
if (major !== undefined && minor !== undefined && build !== undefined) {
release = `${major}.${minor}.${build}`;
}
} catch {
// ignore
} finally {
versionInfo = {
release: release || os.release(),
buildNumber: buildNumber || getWindowsBuildNumberFromOsRelease()
};
}
}
/**
* Gets Windows version information from the registry.
* @returns The Windows version in Major.Minor.Build format (e.g., "10.0.19041")
*/
export async function getWindowsRelease(): Promise<string> {
if (!versionInfo) {
await initWindowsVersionInfo();
}
return versionInfo!.release;
}
/**
* Gets the Windows build number from the registry.
* @returns The Windows build number (e.g., 19041 for Windows 10 2004)
*/
export async function getWindowsBuildNumberAsync(): Promise<number> {
if (!versionInfo) {
await initWindowsVersionInfo();
}
return versionInfo!.buildNumber;
}
/**
* Synchronous version of getWindowsBuildNumberAsync().
* @returns The Windows build number (e.g., 19041 for Windows 10 2004)
*/
export function getWindowsBuildNumberSync(): number {
return versionInfo.buildNumber;
return isWindows ? getWindowsBuildNumberFromOsRelease() : 0;
}
}
/**
* Gets the cached Windows release string synchronously.
* Falls back to os.release() if the cache hasn't been initialized yet.
* @returns The Windows version in Major.Minor.Build format (e.g., "10.0.19041")
*/
export function getWindowsReleaseSync(): string {
return versionInfo?.release ?? os.release();
}
/**
* Parses the Windows build number from os.release().
* This is used as a fallback when registry reading is not available.
*/
function getWindowsBuildNumberFromOsRelease(): number {
const osVersion = (/(\d+)\.(\d+)\.(\d+)/g).exec(os.release());
if (osVersion && osVersion.length === 4) {
return parseInt(osVersion[3], 10);
}
return 0;
}