.NET获取操作系统版本、获取操作系统位数(转载)

添加引用System.Management
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;

namespace 安装界面
{
class OsHelper
{

    //操作系统PlatformID主版本号副版本号   
    public enum OsVer
    {
        Windows95 = 140, Windows98 = 1410, WindowsMe = 1490, WindowsNT35 = 230, WindowsNT40 = 240, Windows2000 = 250, WindowsXP = 251, Windows2003 = 252, WindowsVista = 260, Windows7 = 261, Windows8 = 271
    }

    /// <summary> 
    /// 获取操作系统位数(x32/64) 
    /// </summary> 
    /// <returns>int</returns> 
    public static int GetOSBit()
    {
        try
        {
            string addressWidth = String.Empty;
            ConnectionOptions mConnOption = new ConnectionOptions();
            ManagementScope mMs = new ManagementScope(@"\\localhost", mConnOption);
            ObjectQuery mQuery = new ObjectQuery("select AddressWidth from Win32_Processor");
            ManagementObjectSearcher mSearcher = new ManagementObjectSearcher(mMs, mQuery);
            ManagementObjectCollection mObjectCollection = mSearcher.Get();
            foreach (ManagementObject mObject in mObjectCollection)
            {
                addressWidth = mObject["AddressWidth"].ToString();
            }
            return Int32.Parse(addressWidth);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
            return 32;
        }
    }

    /// <summary>
    /// 获取操作系统版本
    /// </summary>
    /// <returns></returns>
    public static OsVer GetOsVersion()
    {
        //判断 
        string iniPath =Environment.GetFolderPath(Environment.SpecialFolder.System);                                
        //获取系统信息                
        System.OperatingSystem osInfo = System.Environment.OSVersion;                
        //获取操作系统ID                
        System.PlatformID platformID = osInfo.Platform;                
        //获取主版本号                
        int versionMajor = osInfo.Version.Major;                
        //获取副版本号                
        int versionMinor = osInfo.Version.Minor;                
        string osInfor = platformID.GetHashCode().ToString() + versionMajor.ToString() + versionMinor.ToString();       
        return (OsVer)(int.Parse(osInfor));
    }
}

}

    原文作者:weixin_34168880
    原文地址: https://blog.csdn.net/weixin_34168880/article/details/89583524
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞