C # determine whether the operating system is 32-bit or 64-bit

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.