.net/C# 當前操作系統是32位還是64位-總結
判斷整型的長度的方式,只有在AnyCPU編譯模式下才有用。因此更好的辦法是獲取真的地址總線位寬
// 判斷操作系統是32位還是64位
virtual public int PlateFormRunMode
{
get
{
if (IntPtr.Size == 8)
{
return 64;
}
return 32;
}
}
//這里需要引用System.Management,該方法在以Guest用戶登錄的情況下拋出異常:
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)
{
return 32;
}
}
//這里需要引用System.Diagnostics
[DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool IsWow64Process([In] IntPtr hProcess, [Out] out bool lpSystemInfo);
private static bool Is64Bit()
{
bool retVal;
IsWow64Process( Process.GetCurrentProcess().Handle, out retVal);
return retVal;
}
//.net 4.0(3.5)以上的
bool is64Bit;
is64Bit= Environment.Is64BitOperatingSystem;
Console.WriteLine(is64Bit); //true為64位; false為32位