软件编程
位置:首页>> 软件编程>> C#编程>> C#判断系统是32位还是64位的方法

C#判断系统是32位还是64位的方法

作者:双人床  发布时间:2022-04-01 12:44:04 

标签: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,该方法在以Guest用户登录的情况下抛出异常:

C#判断系统是32位还是64位的方法

或者用以下方法:


[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

希望本文所述对大家的C#程序设计有所帮助。

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com