C#代码获取系统版本信息的例子

发布时间:2020-01-17编辑:脚本学堂
c#获取系统版本信息的一段代码,使用System.Management类库取得系统版本信息。

一个获取系统版本信息的c#代码。

复制代码 代码示例:
public class OSInfoMation
{
public static string OSBit()
{
try
{
ConnectionOptions oConn = new ConnectionOptions();
System.Management.ManagementScope managementScope = new System.Management.ManagementScope("\localhost", oConn);
System.Management.ObjectQuery objectQuery = new System.Management.ObjectQuery("select AddressWidth from Win32_Processor");
ManagementObjectSearcher moSearcher = new ManagementObjectSearcher(managementScope, objectQuery);
ManagementObjectCollection moReturnCollection = null;
string addressWidth = null;
moReturnCollection = moSearcher.Get();
foreach (ManagementObject oReturn in moReturnCollection)
{
addressWidth = oReturn["AddressWidth"].ToString();
}
return addressWidth;
}
catch
{
return "获取错误";
}
}
public static string GetOsVersion()
{
string osBitString = OSBit();
string osVersionString = Environment.OSVersion.ToString();
return string.Format(@"系统:{0}。位:{1}", osVersionString, osBitString);
}
}

调用:
 

复制代码 代码示例:
static void Main(string[] args)
{
Console.WriteLine(OSInfoMation.GetOsVersion());
Console.ReadLine();
}