C#判斷作業系統是32位還是64位

來源:互聯網
上載者:User

標籤:style   os   io   for   cti   代碼   div   ar   

1 根據控制代碼長度判斷作業系統是否為64位作業系統public static bool IsRunningOn64Bit { get { return IntPtr.Size == 8; } }2 根據控制代碼長度判斷作業系統是否為64位作業系統(不安全的程式碼)public static unsafe bool IsRunningOn64Bit { get { return (sizeof(IntPtr) == sizeof(long)); } }

將項目做如下設定:項目屬性對話方塊->配置屬性->產生->允許不安全的程式碼塊 設為"true"

3 根據AddressWidth屬性判斷

AddressWidth的值受CPU和作業系統的雙重影響。如下:

  32bit OS 64bit OS
32bit CPU AddressWidth = 32 N/A
64bit CPU AddressWidth = 32 AddressWidth = 64

以下程式段讀取AddressWidth的值

public static string GetAddressWidth() {     ConnectionOptions oConn = new ConnectionOptions();     System.Management.ManagementScope oMs = new System.Management.ManagementScope("\\\\localhost", oConn);     System.Management.ObjectQuery oQuery = new System.Management.ObjectQuery("select AddressWidth from Win32_Processor");     ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs, oQuery);     ManagementObjectCollection oReturnCollection = oSearcher.Get();     string addressWidth = null;    foreach (ManagementObject oReturn in oReturnCollection)     {         addressWidth = oReturn["AddressWidth"].ToString();     }    return addressWidth; }4 判斷作業系統是否為64位作業系統bool IsRunningOn64Bit() { #if defined(_WIN64)  return true; // 64-bit programs run only on Win64 #elif defined(_WIN32)  // 32-bit programs run on both 32-bit and 64-bit Windows  // so must sniff  bool f64 = false;  return IsWow64Process(GetCurrentProcess(), &f64) && f64; #else  return false; // Win64 does not support Win16 #endif }

網友評論:不要用IsWow64Process(),Wow64特指Windows32 On Win64。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.