欢迎来到忆约科技! 手机版
编程专栏
网络探索
科技频道
社会话题
美文鉴赏

判断操作系统是32位还是64位

编辑时间:2016年12月29日 22:25 作者:E小编


引入System.Management,再引用


usingSystem;

usingSystem.Collections.Generic;

usingSystem.ComponentModel;

usingSystem.Data;

usingSystem.Drawing;

usingSystem.Text;

usingSystem.Windows.Forms;

usingSystem.Management;

 

namespaceWindowsFormsApplication1

{

   public partialclass Form1: Form

   {

       public Form1()

       {

            InitializeComponent();

       }

 

       private voidbutton1_Click(object sender, EventArgs e)

       {

            //AddressWidth返回值

            //32bitOS64bit OS

            //32bitCPUAddressWidth = 32N/A

            //64bitCPUAddressWidth = 32AddressWidth = 64

            textBox1.Text =Judgment64or32System().ToString();

       }

       /// <summary>

       /// 判断当前系统是32位的还是64

       /// </summary>

       /// <returns></returns>

       public staticstring Judgment64or32System()

       {

            try

            {

                stringaddressWidth = String.Empty;

                ConnectionOptionsmConnOption = new ConnectionOptions();

                ManagementScopemMs = new ManagementScope("\\\\localhost", mConnOption);

                ObjectQuerymQuery = new ObjectQuery("select AddressWidth from Win32_Processor");

                ManagementObjectSearchermSearcher = new ManagementObjectSearcher(mMs,mQuery);

                ManagementObjectCollectionmObjectCollection = mSearcher.Get();

                foreach(ManagementObject mObject in mObjectCollection)

                {

                    addressWidth = mObject["AddressWidth"].ToString();

                }

                returnaddressWidth;//返回值

            }

            catch(Exception ex)

            {

                Console.WriteLine(ex.Message.ToString());

                returnString.Empty;

            }

       }

   }

}

二维码