C++
Linux系统(尝试使用POSIX uname函数)
Windows系统(尝试使用Windows API函数)
用#ifdef _WIN32整合一下
#include <iostream>
#ifdef _WIN32
// Windows includes
#include <windows.h>
#else
// POSIX (Unix, Linux, macOS, etc.) includes
#include <unistd.h>
#include <sys/utsname.h>
#endif
int main()
{
#ifdef _WIN32
// Windows-specific code
OSVERSIONINFOEX osvi;
SYSTEM_INFO si;
BOOL bOsVersionInfoEx;
ZeroMemory(&si, sizeof(SYSTEM_INFO));
ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
bOsVersionInfoEx = GetVersionEx((OSVERSIONINFO*)&osvi);
if (!bOsVersionInfoEx) {
std::cerr << "Failed to get OS version information." << std::endl;
return 1;
}
GetSystemInfo(&si);
std::cout << "Current C++ Standard: " << __cplusplus << std::endl;
std::cout << "Operating system: " << "Windows" << std::endl;
std::cout << "Major version: " << osvi.dwMajorVersion << std::endl;
std::cout << "Minor version: " << osvi.dwMinorVersion << std::endl;
std::cout << "Build number: " << osvi.dwBuildNumber << std::endl;
std::cout << "Platform: " << osvi.dwPlatformId << std::endl;
std::cout << "Service pack: "
<< (osvi.szCSDVersion[0] ? osvi.szCSDVersion : "None")
<< std::endl;
switch (si.wProcessorArchitecture) {
case PROCESSOR_ARCHITECTURE_AMD64:
std::cout << "Machine: AMD64" << std::endl;
break;
case PROCESSOR_ARCHITECTURE_INTEL:
std::cout << "Machine: x86" << std::endl;
break;
case PROCESSOR_ARCHITECTURE_ARM:
std::cout << "Machine: ARM" << std::endl;
break;
case PROCESSOR_ARCHITECTURE_IA64:
std::cout << "Machine: IA64" << std::endl;
break;
case PROCESSOR_ARCHITECTURE_ARM64:
std::cout << "Machine: ARM64" << std::endl;
break;
default:
std::cout << "Machine: Unknown architecture" << std::endl;
break;
}
#else
// Unix/POSIX-specific code
struct utsname unameData;
if (uname(&unameData) != 0) {
std::cerr << "Failed to get system information." << std::endl;
return 1;
}
std::cout << "Current C++ Standard: " << __cplusplus << std::endl;
std::cout << "Operating system: " << unameData.sysname << std::endl;
std::cout << "Node name: " << unameData.nodename << std::endl;
std::cout << "Release: " << unameData.release << std::endl;
std::cout << "Version: " << unameData.version << std::endl;
std::cout << "Machine: " << unameData.machine << std::endl;
#endif
return 0;
}
Current C++ Standard: 201402
Operating system: Linux
Node name: tio2
Release: 5.2.11-100.fc29.x86_64
Version: #1 SMP Thu Aug 29 12:52:22 UTC 2019
Machine: x86_64
Current C++ Standard: 201703
Operating system: Windows
Major version: 10
Minor version: 0
Build number: 19045
Platform: 2
Service pack: None
Machine: AMD64
Scala
object Main {
def main(args: Array[String]): Unit = {
println(s"Scala version: ${util.Properties.versionString}")
println(s"OS name: ${System.getProperty("os.name")}")
println(s"OS version: ${System.getProperty("os.version")}")
println(s"OS architecture: ${System.getProperty("os.arch")}")
}
}
Scala version: version 2.12.10
OS name: Windows 10
OS version: 10.0
OS architecture: amd64
Python
import platform
import sys
# Python version
print(f"Python version: {sys.version}")
# OS Information
print(f"OS: {platform.system()} {platform.release()}")
Python version: 3.8.0b4 (default, Sep 2 2019, 08:08:37)
[GCC 8.3.1 20190223 (Red Hat 8.3.1-2)]
OS: Linux 5.2.11-100.fc29.x86_64
Java
public class Main {
public static void main(String[] args) {
// Java version
String javaVersion = System.getProperty("java.version");
System.out.println("Java version: " + javaVersion);
// Operating system information
String osName = System.getProperty("os.name");
String osVersion = System.getProperty("os.version");
String osArch = System.getProperty("os.arch");
System.out.println("OS Name: " + osName);
System.out.println("OS Version: " + osVersion);
System.out.println("OS Architecture: " + osArch);
}
}
Java version: 1.8.0_222
OS Name: Linux
OS Version: 5.2.11-100.fc29.x86_64
OS Architecture: amd64
Erlang(escript)
#!/usr/bin/env escript
%% -*- erlang -*-
main(_) ->
%% Erlang version
ErlangVersion = erlang:system_info(otp_release),
io:format("Erlang version: ~s~n", [ErlangVersion]),
%% OS Information
{OSFamily, OSName} = os:type(),
io:format("OS: ~s ~s~n", [OSFamily, OSName]),
%% OS Version (Only works on UNIX systems, will return undefined on others)
OSVersion = os:version(),
io:format("OS Version: ~p~n", [OSVersion]).
Erlang version: 20
OS: unix linux
OS Version: {5,2,11}
JavaScript
console.log('JavaScript Standard:', process.versions.v8);
console.log('Compiler/Engine:', process.versions.node);
console.log('OS:', process.platform + ' ' + process.arch);
JavaScript Standard: 11.3.244.8-node.23
Compiler/Engine: 20.18.0
OS: linux x64
Go
package main
import (
"fmt"
"runtime"
)
func main() {
// Go version
fmt.Println("Go version: " + runtime.Version())
// OS Information
fmt.Println("OS: " + runtime.GOOS)
fmt.Println("Architecture: " + runtime.GOARCH)
}
Go version: go1.11.13
OS: linux
Architecture: amd64
OCaml
let () =
(* OCaml version *)
let ocaml_version = Sys.ocaml_version in
Printf.printf "OCaml version: %s\n" ocaml_version;
(* OS Information *)
let os_type = Sys.os_type in
Printf.printf "OS Type: %s\n" os_type
OCaml version: 4.07.0
OS Type: Unix
Racket
#lang racket
(define racket-version (version))
(define os-info (system-type))
(printf "Racket version: ~a\n" racket-version)
(printf "OS information: ~a\n" os-info)
Racket version: 6.8
OS information: unix
C#
using System;
using System.Runtime.InteropServices;
class Program
{
static void Main()
{
// .NET version
var dotnetVersion = Environment.Version;
Console.WriteLine($"C# / .NET version: {dotnetVersion}"); //你可以使用.NET的内置方法和属性来获取C#(更准确的说是.NET的版本)
// Operating system information
var osDescription = RuntimeInformation.OSDescription;
var osArchitecture = RuntimeInformation.OSArchitecture;
Console.WriteLine($"OS: {osDescription}");
Console.WriteLine($"Architecture: {osArchitecture}");
}
}
C# / .NET version: 4.0.30319.42000
OS: Linux 5.2.11-100.fc29.x86_64 #1 SMP Thu Aug 29 12:52:22 UTC 2019
Architecture: X64
Ruby
# Ruby version
puts "Ruby version: #{RUBY_VERSION}"
# OS Information
require 'rbconfig'
os = RbConfig::CONFIG['host_os']
puts "OS: #{os}"
Ruby version: 2.5.5
OS: linux-gnu
Rust
// [dependencies]
// sys-info = "0.9"
use sys_info;
fn main() {
// Rust version
println!("Rust version: {}", env!("CARGO_PKG_VERSION"));
// OS Information
match sys_info::os_type() {
Ok(os_type) => println!("OS: {}", os_type),
Err(e) => eprintln!("Failed to get OS type: {}", e),
}
match sys_info::os_release() {
Ok(os_release) => println!("OS Release: {}", os_release),
Err(e) => eprintln!("Failed to get OS release: {}", e),
}
}
Rust version: 0.1.0
OS: Windows
OS Release: 6.2.9200
Elixir
# Elixir version
IO.puts "Elixir version: #{System.version()}"
# Erlang/OTP version
# {:ok, otp_version} = :otp_release.erlang()
# IO.puts "Erlang/OTP version: #{otp_version}"
# OS Information
os_type = :os.type()
IO.puts "OS: #{inspect(os_type)}"
Elixir version: 1.7.2
OS: {:unix, :linux}
Mathematica
Print["Mathematica version: ", $Version];
Print["Operating system: ", $OperatingSystem];
Mathematica version: 12.0.1 for Linux x86 (64-bit) (October 16, 2019)
Operating system: Unix
Swift(存疑)
import Foundation
// Swift version is typically determined by the compiler and isn't available at runtime.
// However, you can use the Swift compiler directive to print it during compilation.
#if swift(>=5.5)
print("Swift version: 5.5 or later")
#elseif swift(>=5.4)
print("Swift version: 5.4")
#elseif swift(>=5.3)
print("Swift version: 5.3")
// add more checks if needed
#else
print("Swift version: unknown")
#endif
// Operating system information
let os = ProcessInfo.processInfo.operatingSystemVersion
print("OS: \(os)")
Swift version: unknown
OS: OperatingSystemVersion(majorVersion: -1, minorVersion: 0, patchVersion: 0)
PHP
<?php
// PHP version
$phpVersion = phpversion();
echo "PHP version: " . $phpVersion . "\n";
// Operating system information
$osInfo = php_uname();
echo "OS information: " . $osInfo . "\n";
?>
PHP version: 7.4.0-dev
OS information: Linux tio2 5.2.11-100.fc29.x86_64 #1 SMP Thu Aug 29 12:52:22 UTC 2019 x86_64
Dart
import 'dart:io';
void main() {
// Dart version
var dartVersion = Platform.version;
print('Dart version: $dartVersion');
// Operating system information
var osInfo = Platform.operatingSystem;
print('Operating system: $osInfo');
}
Dart version: 2.4.1 (Wed Aug 7 13:15:56 2019 +0200) on "linux_x64"
Operating system: linux
Kotlin
fun main() {
// Kotlin version
val kotlinVersion = KotlinVersion.CURRENT
println("Kotlin version: $kotlinVersion")
// Operating system information
val osName = System.getProperty("os.name")
println("Operating system: $osName")
}
Kotlin version: 1.3.31
Operating system: Linux
R
# R version
cat("R version:", R.version.string, "\n")
# Operating system information
sys_info <- Sys.info()
cat("Operating system:", sys_info["sysname"], "\n")
cat("Version:", sys_info["release"], "\n")
R version: R version 3.5.2 (2018-12-20)
Operating system: Linux
Version: 5.2.11-100.fc29.x86_64
Julia
# Julia version
println("Julia version: ", VERSION)
# Operating system information
println("Operating system: ", Sys.KERNEL)
Julia version: 1.0.0
Operating system: Linux
Pari/GP(存疑)
/* PARI/GP version */
print("PARI/GP version: ", version())
/* Operating system information */
print("Operating system: ", getenv("OS"))
PARI/GP version: [2, 11, 1]
Operating system: 0
Octave
% Octave version
disp(['Octave version: ' version])
% Operating system information
disp(['Operating system: ' computer])
Octave version: 4.2.2
Operating system: x86_64-redhat-linux-gnu
Zig
const std = @import("std");
const builtin = @import("builtin");
pub fn main() !void {
std.log.info("Zig version: {}", .{ builtin.zig_version });
}
info: Zig version: 0.11.0
Perl
use strict;
print "Operating System: $^O\n";
print "Perl Version: $]\n";
Operating System: linux
Perl Version: 5.040000
Tcl
# Print the operating system information
puts "Operating System: $tcl_platform(os)"
# Print the Tcl version
puts "Tcl Version: [info tclversion]"
Operating System: Linux
Tcl Version: 8.6
Fortran (存疑)
program system_info
use iso_fortran_env
implicit none
! Print the Fortran compiler version and standard
print *, "Fortran Compiler: ", compiler_version()
print *, "Fortran Standard: ", compiler_options()
! Operating system information (example using a system call)
print *, "Operating System: ", get_os()
contains
! Function to retrieve OS information (simplistic example)
function get_os() result(os_name)
character(len=32) :: os_name
! Use preprocessor macros to determine OS
! These macros are often defined by the compiler
! and may vary depending on the system and compiler.
#ifdef _WIN32
os_name = "Windows"
#elif __linux__
os_name = "Linux"
#elif __APPLE__
os_name = "MacOS"
#else
os_name = "Unknown OS"
#endif
end function get_os
! Function to get compiler version (optional, compiler-specific)
function compiler_version() result(version)
character(len=128) :: version
! Some compilers may provide built-in definitions
#ifdef __GFORTRAN__
version = "GNU Fortran"
#elif __INTEL_COMPILER
version = "Intel Fortran"
#elif __PGI
version = "PGI Fortran"
#elif __NVCOMPILER
version = "NVIDIA HPC Compiler"
#else
version = "Unknown Compiler"
#endif
end function compiler_version
! Function to get Fortran standard (compiler-specific)
function compiler_options() result(options)
character(len=128) :: options
#ifdef _OPENMP
options = "OpenMP, "
#else
options = ""
#endif
#ifdef __GFORTRAN__
options = trim(options) // "GNU-specific Flags"
#else
options = trim(options) // "Standard Fortran"
#endif
end function compiler_options
end program system_info
Compiler: NVIDIA HPC Compiler
Fortran Standard: Fortran 95 or earlier
MySQL
-- MySQL 系统信息查询
-- =============================================
-- 操作系统信息
SELECT
@@version_compile_os AS 'Operating System',
@@version_compile_machine AS 'Machine Architecture',
@@version AS 'MySQL Version',
@@hostname AS 'Hostname',
@@port AS 'Port',
@@basedir AS 'Base Directory',
@@datadir AS 'Data Directory',
@@character_set_server AS 'Server Character Set',
@@collation_server AS 'Server Collation',
@@character_set_database AS 'Database Character Set',
@@collation_database AS 'Database Collation';
-- -- 更多系统变量
-- SHOW VARIABLES LIKE 'version%';
-- SHOW VARIABLES LIKE 'character_set%';
-- SHOW VARIABLES LIKE 'collation%';
| Operating System |
Machine Architecture |
MySQL Version |
Hostname |
Port |
Base Directory |
Data Directory |
Server Character Set |
Server Collation |
Database Character Set |
Database Collation |
| Linux |
x86_64 |
8.0.40 |
localhost |
3306 |
/usr/ |
/var/lib/mysql/ |
utf8mb4 |
utf8mb4_0900_ai_ci |
utf8mb3 |
utf8mb3_general_ci |
| Variable_name |
Value |
| version |
8.0.40 |
| version_comment |
MySQL Community Server - GPL |
| version_compile_machine |
x86_64 |
| version_compile_os |
Linux |
| version_compile_zlib |
1.3.1 |
MS SQL Server
-- SQL Server 系统信息查询
SELECT
@@version AS 'MS SQL Server Version'
-- =============================================
-- 操作系统信息
-- SELECT
-- SERVERPROPERTY('ComputerNamePhysicalNetBIOS') AS 'Computer Name',
-- SERVERPROPERTY('ServerName') AS 'Server Name',
-- SERVERPROPERTY('MachineName') AS 'Machine Name',
-- SERVERPROPERTY('ProductVersion') AS 'SQL Server Version',
-- SERVERPROPERTY('ProductLevel') AS 'Product Level',
-- SERVERPROPERTY('Edition') AS 'Edition',
-- SERVERPROPERTY('InstanceName') AS 'Instance Name';
-- Windows版本信息
-- SELECT
-- windows_release,
-- windows_service_pack_level,
-- windows_sku,
-- os_language_version
-- FROM sys.dm_os_windows_info;
-- -- SQL Server配置信息
-- SELECT
-- SERVERPROPERTY('Collation') AS 'Server Collation',
-- SERVERPROPERTY('SqlCharSet') AS 'Character Set',
-- SERVERPROPERTY('SqlCharSetName') AS 'Character Set Name',
-- SERVERPROPERTY('SqlSortOrder') AS 'Sort Order',
-- SERVERPROPERTY('SqlSortOrderName') AS 'Sort Order Name';
-- -- 操作系统详细信息
-- SELECT
-- cpu_count,
-- hyperthread_ratio,
-- physical_memory_kb,
-- virtual_memory_kb,
-- committed_kb,
-- committed_target_kb
-- FROM sys.dm_os_sys_info;
| MS SQL Server Version |
| Microsoft SQL Server 2019 (RTM-CU28-GDR) (KB5046060) - 15.0.4395.2 (X64) Sep 24 2024 07:38:17 Copyright (C) 2019 Microsoft Corporation Express Edition (64-bit) on Linux (Ubuntu 20.04.6 LTS) |
Oracle
-- Oracle 系统信息查询
-- =============================================
-- 操作系统信息
with
t1 as(
SELECT
BANNER AS "Oracle Version"
FROM v$version
)
,
-- t2 as(
-- -- 数据库和实例信息
-- SELECT
-- name AS "Database Name",
-- platform_name AS "Platform",
-- created AS "Created Date"
-- FROM v$database)
-- ,
-- 实例信息
-- t3 as(
-- SELECT
-- instance_name AS "Instance Name",
-- host_name AS "Host Name",
-- version AS "Version",
-- startup_time AS "Startup Time",
-- status AS "Status"
-- FROM v$instance)
-- ,
-- 字符集信息
t4 as(
SELECT
parameter AS "Parameter",
value AS "Value"
FROM nls_database_parameters
WHERE parameter IN ('NLS_CHARACTERSET', 'NLS_NCHAR_CHARACTERSET', 'NLS_LANGUAGE', 'NLS_TERRITORY')
)
,
-- 当前会话NLS参数
t5 as(
SELECT
parameter AS "Parameter",
value AS "Value"
FROM nls_session_parameters
WHERE parameter IN ('NLS_LANGUAGE', 'NLS_TERRITORY', 'NLS_CHARACTERSET')
)
-- ,
-- -- 操作系统统计信息
-- t6 as
-- (
-- SELECT
-- stat_name AS "Statistic",
-- value AS "Value"
-- FROM v$osstat
-- WHERE stat_name IN ('NUM_CPUS', 'PHYSICAL_MEMORY_BYTES', 'LOAD')
-- )
SELECT * from t1
| Oracle Version |
| Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production |
| PL/SQL Release 11.2.0.2.0 - Production |
| CORE 11.2.0.2.0 Production |
| TNS for Linux: Version 11.2.0.2.0 - Production |
| NLSRTL Version 11.2.0.2.0 - Production |
PostgresSQL
-- PostgreSQL 系统信息查询
-- =============================================
-- 版本信息
SELECT version() AS "PostgreSQL Version";
-- 系统设置信息
-- SELECT
-- name AS "Parameter",
-- setting AS "Value",
-- unit AS "Unit",
-- category AS "Category"
-- FROM pg_settings
-- WHERE category LIKE '%Locale%' OR category LIKE '%Client%'
-- ORDER BY category, name;
-- -- 字符集和排序规则
-- SELECT
-- datname AS "Database",
-- pg_encoding_to_char(encoding) AS "Encoding",
-- datcollate AS "Collate",
-- datctype AS "Ctype"
-- FROM pg_database
-- WHERE datname = current_database();
-- -- 服务器配置
SELECT
name AS "Parameter",
setting AS "Value",
unit AS "Unit"
FROM pg_settings
WHERE name IN ('server_version', 'server_encoding', 'client_encoding', 'timezone', 'log_timezone')
ORDER BY name;
-- -- 统计信息
-- SELECT
-- schemaname AS "Schema",
-- tablename AS "Table",
-- attname AS "Column",
-- n_distinct AS "Distinct Values",
-- correlation AS "Correlation"
-- FROM pg_stats
-- WHERE schemaname = 'information_schema'
-- LIMIT 10;
-- -- 数据库大小
-- SELECT
-- pg_database.datname AS "Database",
-- pg_size_pretty(pg_database_size(pg_database.datname)) AS "Size"
-- FROM pg_database
-- ORDER BY pg_database_size(pg_database.datname) DESC;
{
"headers": [
"PostgreSQL Version"
],
"values": [
[
"PostgreSQL 16.6 (Ubuntu 16.6-1.pgdg20.04+1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0, 64-bit"
]
]
}
{
"headers": [
"Parameter",
"Value",
"Unit"
],
"values": [
[
"client_encoding",
"UTF8",
null
],
[
"log_timezone",
"Etc/UTC",
null
],
[
"server_encoding",
"UTF8",
null
],
[
"server_version",
"16.6 (Ubuntu 16.6-1.pgdg20.04+1)",
null
]
]
}
PowerShell
# PowerShell Version Checker Script
Write-Host "=== PowerShell Version Information ===" -ForegroundColor Green
Write-Host "PowerShell Version: $($PSVersionTable.PSVersion)" -ForegroundColor Yellow
Write-Host "PowerShell Edition: $($PSVersionTable.PSEdition)" -ForegroundColor Yellow
Write-Host "Runtime: $($PSVersionTable.Runtime)" -ForegroundColor Yellow
Write-Host "OS: $($PSVersionTable.OS)" -ForegroundColor Yellow
Write-Host "`n=== Full Version Table ===" -ForegroundColor Green
$PSVersionTable | Format-List
=== PowerShell Version Information ===
PowerShell Version: 6.2.3
PowerShell Edition: Core
Runtime:
OS: Linux 5.2.11-100.fc29.x86_64 #1 SMP Thu Aug 29 12:52:22 UTC 2019
=== Full Version Table ===
Name : PSVersion
Value : 6.2.3
Name : PSEdition
Value : Core
Name : GitCommitId
Value : 6.2.3
Name : OS
Value : Linux 5.2.11-100.fc29.x86_64 #1 SMP Thu Aug 29 12:52:22 UTC 2019
Name : Platform
Value : Unix
Name : PSCompatibleVersions
Value : {1.0, 2.0, 3.0, 4.0…}
Name : PSRemotingProtocolVersion
Value : 2.3
Name : SerializationVersion
Value : 1.1.0.1
Name : WSManStackVersion
Value : 3.0
Pascal (Free Pascal / Lazarus)
program SystemInfo;
{$IFDEF FPC}
{$MODE OBJFPC}
{$ENDIF}
uses
{$IFDEF FPC}
sysutils
{$ELSE}
sysutils
{$ENDIF};
begin
// Language Standard Information
writeln('=== Language Standard Info ===');
{$IFDEF FPC}
writeln('Compiler: Free Pascal');
writeln('Version: ', {$I %FPCVERSION%});
writeln('Target CPU: ', {$I %FPCTARGETCPU%});
writeln('Target OS: ', {$I %FPCTARGETOS%});
{$ENDIF}
{$IFDEF VER30}
writeln('Delphi Version: XE');
{$ENDIF}
{$IFDEF VER26}
writeln('Delphi Version: XE5');
{$ENDIF}
// Operating System Information
writeln;
writeln('=== Operating System Info ===');
{$IFDEF WINDOWS}
writeln('Operating System: Windows');
{$ENDIF}
{$IFDEF LINUX}
writeln('Operating System: Linux');
{$ENDIF}
{$IFDEF UNIX}
writeln('Operating System: Unix');
{$ENDIF}
{$IFDEF DARWIN}
writeln('Operating System: macOS/Darwin');
{$ENDIF}
// Architecture
{$IFDEF CPU32}
writeln('Architecture: 32-bit');
{$ENDIF}
{$IFDEF CPU64}
writeln('Architecture: 64-bit');
{$ENDIF}
writeln('Program Size: ', ParamStr(0));
readln;
end.
Free Pascal Compiler version 3.2.2+dfsg-32 [2024/01/05] for x86_64
Copyright (c) 1993-2021 by Florian Klaempfl and others
Target OS: Linux for x86-64
Compiling main.pas
Linking a.out
57 lines compiled, 0.1 sec
=== Language Standard Info ===
Compiler: Free Pascal
Version: 3.2.2
Target CPU: x86_64
Target OS: Linux
=== Operating System Info ===
Operating System: Linux
Operating System: Unix
Architecture: 64-bit
Program Size: /home/a.out