1. Determine whether the operating system is a 64-bit public static bool isrunningon64bit {get {return intptr Based on the handle length. size = 8 ;}} 2 determine whether the operating system is a 64-bit (Insecure code) public static unsafe bool isrunningon64bit Based on the handle length {get {return (sizeof (intptr) ==sizeof (long ));}}
Set the project as follows: Project Properties dialog box-> Configuration properties-> Generate-> allow Insecure code blocks to "true"
3. Based on the addresswidth attribute
The addresswidth value is affected by the CPU and operating system. As follows:
|
32bit OS |
64bit OS |
32bit CPU |
Addresswidth = 32 |
N/ |
64-bit CPU |
Addresswidth = 32 |
Addresswidth = 64 |
The following program segment reads the addresswidth Value
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. Determine whether the operating system is a 64-bit operating system 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}
Comments from netizens: Do not use iswow64process (). wow64 refers to windows32 on win64.