c#如何判断系统位数?c#判断系统是32位还是64位
方法一:
复制代码 代码示例:
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.Management,以判断系统位数是32位还是64位?
该方法在以Guest用户登录的情况下抛出异常:
方法二:
复制代码 代码示例:
[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;
}
此处需要引用System.Diagnostics。