我想知道 PC 有哪个 Windows 版本.. 在 C# Framework 3.5 中
我试过使用
操作系统 os = Environment.OSVersion;
版本ver = os.Version;
但结果是
平台:WIN32NT
版本 6.2.9200
次要版本:2
主要版本:6
问题是我有“Windows 8 Pro”...
我怎样才能检测到它?
谢谢
我想知道 PC 有哪个 Windows 版本.. 在 C# Framework 3.5 中
我试过使用
操作系统 os = Environment.OSVersion;
版本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 中添加了新功能。不要使用 Version API Helper 函数来确定操作系统平台或版本号,而是测试功能本身是否存在。
在我的场景中,我需要我的应用程序来捕获计算机信息以获取可能的错误报告和统计信息。
我没有找到必须添加应用程序清单的解决方案令人满意。不幸的是,我在谷歌搜索时发现的大多数建议都表明了这一点。
问题是,当使用清单时,必须手动将每个操作系统版本添加到其中,以便该特定操作系统版本能够在运行时报告自己。
换句话说,这变成了一种竞争条件:我的应用程序的用户很可能正在使用我的应用程序的一个版本,该版本早于正在使用的操作系统。当微软推出新的操作系统版本时,我必须立即升级应用程序。我还必须强制用户在更新操作系统的同时升级应用程序。
换句话说,不是很可行。
浏览选项后,我发现了一些建议使用注册表查找的参考(与应用程序清单相比很少)。
我的(切碎的)ComputerInfo
类只有WinMajorVersion
,WinMinorVersion
和IsServer
属性如下所示:
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";
}