How do I know if my processor is 32 or 64 bit in C #?

How to find out that my processor has 32 or 64 bits in C #?

+6
c #
source share
8 answers

You can query the Win32_Processor WMI Win32_Processor using System.Management.ManagementObject :

 ManagementObject mo; mo = new ManagementObject("Win32_Processor.DeviceID='CPU0'"); ushort i = (ushort)mo["Architecture"]; switch (i) { case 0: return "32 Bit"; break; case 9: return "64 Bit"; break; } 

See the MSDN library for other processor codes.

The problem is that the user who runs the program needs privileges to view WMI.

+5
source share

For those using .NET 4.0 or later, there is a built-in property System.Environment.Is64BitOperatingSystem to provide you with this information.

+6
source share

The easiest way to check C #:

 var size = IntPtr.Size; 

the size will be either 4 or 8, but the bigger question is, why should you know?

+5
source share

You can get information about the CPU (and more) from the registry:

 HKLM\HARDWARE\DESCRIPTION\System\CentralProcessor 

It has 1 or more subsections (one for each core) called 0, 1, 2, etc. Each subsection contains information about the kernel, including an Identifier, which contains either x86 or slightly 64-bit text, including the processor family and model.

+3
source share

If you want to know if the current application is running as a 32-bit application or a 64-bit application, just use the IntPtr.Size property.

 int bits = IntPtr.Size * 8; 

Finding out information about the processor itself is more difficult. Here is a library that receives some information, but the page does not indicate whether it receives this specific information. You can get some information from the registry , I did not check how much information exists.

+2
source share

You can also use P / Invoke to call GetNativeSystemInfo and get the SystemInfo structure.

Another solution looks like this:

 isWow64 = false; if (System.Environment.OSVersion.Version.Major >= 5 && System.Environment.OSVersion.Version.Minor >= 1) { var processHandle = GetProcessHandle((uint) System.Diagnostics.Process.GetCurrentProcess().Id); bool retVal; if (!NativeMethods.IsWow64Process(processHandle, out retVal)) { throw (new Win32Exception()); } isWow64 = retVal; } 

Alternative solution (but not recommended) :)

 public bool Is64bitOS { get { return Environment.GetEnvironmentVariable("ProgramFiles(x86)") != null; } } public string ProgramFilesX86 { get { string programFiles = Environment.GetEnvironmentVariable("ProgramFiles(x86)"); if (programFiles == null) { programFiles = Environment.GetEnvironmentVariable( "ProgramFiles"); } return programFiles; } } 
+2
source share

Request Win32_Processor class via WMI

+2
source share
 [DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool IsWow64Process([In] IntPtr hProcess, [Out] out bool lpSystemInfo); public static bool Is64Bit() { bool retVal; IsWow64Process(Process.GetCurrentProcess().Handle, out retVal); return retVal; } 
0
source share

All Articles