C# 获取当前操作系统是x64(64位)还是x32(32位)

C# 获取当前操作系统是x64(64位)还是x32(32位)
using System;
using System.Management;

namespace DemoConsole
{
    class Program
    {
        // 引入System.Management
        static void Main(string[] args)
        {
            Console.WriteLine("OSBit : {0}", GetOSBit());

            Console.Read();
        }

        /// <summary>
        /// 获取当前操作系统是x64(64位)还是x32(32位)
        /// </summary>
        /// <returns></returns>
        public static int GetOSBit()
        {
            try
            {
                string addressWidth = "";
                ConnectionOptions connectionOptions = new ConnectionOptions();
                ManagementScope managementScope = new ManagementScope(@"\\localhost", connectionOptions);
                ObjectQuery objectQuery = new ObjectQuery("select AddressWidth from Win32_Processor");
                ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher(managementScope, objectQuery);
                ManagementObjectCollection managementObjectCollection = managementObjectSearcher.Get();
                foreach (ManagementObject managementObject in managementObjectCollection)
                {
                    addressWidth = managementObject["AddressWidth"].ToString();
                }

                return int.Parse(addressWidth);
            }
            catch
            {
                throw;
            }
        }
    }
}


作者最新文章
C# 删除一维数组内的某个值(元素), C# 删除数组元素的方法
C# 如何判断一个字符串是否包含另一个字符串的方法,C# IndexOf 的使用
Winform 只能输入整数的TextBox (文本框) 控件
Winform 禁止重复启动,单实例exe程序
Winform TextBox (文本框) 控件禁止输入内容