当前位置: 编程技术>.net/c#/asp.net
c# 获取系统版本信息的代码
来源: 互联网 发布时间:2014-08-30
本文导语: 在一些场景下,我们需要获取到机器的系统版本信息,以下代码提供了一个完整的示例,供大家参考。 代码示例: ///获取系统版本信息 public class OSInfoMation { public static string OSBit() { try { ConnectionOptions oConn = new ConnectionOptions(...
在一些场景下,我们需要获取到机器的系统版本信息,以下代码提供了一个完整的示例,供大家参考。
代码示例:
///获取系统版本信息
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();
} //www.heatpress123.net
return addressWidth;
}
catch
{
return "获取错误";
}
}
public static string GetOsVersion()
{
string osBitString = OSBit();
string osVersionString = Environment.OSVersion.ToString();
return string.Format(@"系统:{0}。位:{1}", osVersionString, osBitString);
}
}
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();
} //www.heatpress123.net
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();
}
{
Console.WriteLine(OSInfoMation.GetOsVersion());
Console.ReadLine();
}