我想知道电脑运行的Windows版本,使用的是C# Framework 3.5。
我试过下面这段代码:
OperatingSystem os = Environment.OSVersion;
Version ver = os.Version;
但结果却是:
平台:WIN32NT
版本:6.2.9200
版本次要号:2
版本主要号:6
问题在于我用的是“Windows 8 Pro”...
如何检测它呢?
谢谢
我想知道电脑运行的Windows版本,使用的是C# Framework 3.5。
我试过下面这段代码:
OperatingSystem os = Environment.OSVersion;
Version ver = os.Version;
但结果却是:
平台:WIN32NT
版本:6.2.9200
版本次要号:2
版本主要号:6
问题在于我用的是“Windows 8 Pro”...
如何检测它呢?
谢谢
您需要自行匹配版本号与相应的字符串值。
以下是最新的Windows操作系统及其对应的版本号列表:
*对于已为Windows 8.1或10显式清单化的应用程序。未显式清单化8.1/10的应用程序将返回Windows 8操作系统版本值(6.2)。
同样来自于相同的出处:
通常,识别当前操作系统并不是确定特定操作系统功能是否存在的最佳方法。这是因为操作系统可以在可重分发的DLL中添加新功能。与其使用版本API Helper函数来确定操作系统平台或版本,不如使用数字,测试特性本身的存在。
using Microsoft.Win32;
namespace Inspection
{
/// <summary>
/// Static class that adds convenient methods for getting information on the running computers basic hardware and os setup.
/// </summary>
public static class ComputerInfo
{
/// <summary>
/// Returns the Windows major version number for this computer.
/// </summary>
public static uint WinMajorVersion
{
get
{
dynamic major;
// The 'CurrentMajorVersionNumber' string value in the CurrentVersion key is new for Windows 10,
// and will most likely (hopefully) be there for some time before MS decides to change this - again...
if (TryGeRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentMajorVersionNumber", out major))
{
return (uint) major;
}
// When the 'CurrentMajorVersionNumber' value is not present we fallback to reading the previous key used for this: 'CurrentVersion'
dynamic version;
if (!TryGeRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentVersion", out version))
return 0;
var versionParts = ((string) version).Split('.');
if (versionParts.Length != 2) return 0;
uint majorAsUInt;
return uint.TryParse(versionParts[0], out majorAsUInt) ? majorAsUInt : 0;
}
}
/// <summary>
/// Returns the Windows minor version number for this computer.
/// </summary>
public static uint WinMinorVersion
{
get
{
dynamic minor;
// The 'CurrentMinorVersionNumber' string value in the CurrentVersion key is new for Windows 10,
// and will most likely (hopefully) be there for some time before MS decides to change this - again...
if (TryGeRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentMinorVersionNumber",
out minor))
{
return (uint) minor;
}
// When the 'CurrentMinorVersionNumber' value is not present we fallback to reading the previous key used for this: 'CurrentVersion'
dynamic version;
if (!TryGeRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentVersion", out version))
return 0;
var versionParts = ((string) version).Split('.');
if (versionParts.Length != 2) return 0;
uint minorAsUInt;
return uint.TryParse(versionParts[1], out minorAsUInt) ? minorAsUInt : 0;
}
}
/// <summary>
/// Returns whether or not the current computer is a server or not.
/// </summary>
public static uint IsServer
{
get
{
dynamic installationType;
if (TryGeRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "InstallationType",
out installationType))
{
return (uint) (installationType.Equals("Client") ? 0 : 1);
}
return 0;
}
}
private static bool TryGeRegistryKey(string path, string key, out dynamic value)
{
value = null;
try
{
var rk = Registry.LocalMachine.OpenSubKey(path);
if (rk == null) return false;
value = rk.GetValue(key);
return value != null;
}
catch
{
return false;
}
}
}
}
我发布了OsInfo nuget,方便比较Windows版本。
bool win8OrLess = Environment.OSVersion.IsLessThanOrEqualTo(OsVersion.Win8);
bool winXp = Environment.OSVersion.IsEqualTo(OsVersion.WinXP);
int? servicePack = Environment.OSVersion.GetServicePackVersion();
bool is64bit = Environment.OSVersion.Is64Bit(); // Already covered in .NET 4.5+
试试这个:
using System.Management;
private string fnGetFriendlyName()
{
var name = (from x in new ManagementObjectSearcher("SELECT Caption FROM Win32_OperatingSystem").Get().OfType<ManagementObject>()
select x.GetPropertyValue("Caption")).FirstOrDefault();
return name != null ? name.ToString() : "Unknown";
}
在IT技术中,“GIL”是全局解释器锁的缩写。它是CPython解释器的一部分,控制同一时刻只有一个线程可以执行Python字节码。这意味着即使在多核CPU上运行Python程序,也不会实现真正的并行处理。因此,在需要高性能的情况下,应该使用多进程或其他并发模型来代替多线程。