欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-09 13:42:32
本文实例讲述了c#判断系统是32位还是64位的方法。分享给大家供大家参考。具体如下: public static int getosbit() { try...

本文实例讲述了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#程序设计有所帮助。