How to determine the bit depth of the operating system by means of.NET?

There is a C# application for Win 7, during the execution of which you need to determine whether it is running on the 32 or 64 bit version of the OS. Is it possible to do this with .NET or is it necessary to use "workarounds"?

Author: Андрей NOP, 2012-07-13

6 answers

Find out the bit depth of the OS (32 or 64 bit) directly by means of .NET is not possible, but there are ways to use WinAPI:

  1. Using the function IsWow64Process

    public static string GetOSBit() {
        bool is64bit = Is64Bit();
        if (is64bit)
            return "x64";
        else
            return "x32";
        }
    
    [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;
    }
    
  2. Using the type size IntPtr

    using System;
    using System.Runtime.InteropServices;
    class OSBitChecker
    {
        static bool Is64Bit
        {
            get { return Marshal.SizeOf(typeof(IntPtr)) == 8; }
        }
    }
    
 3
Author: EVGEN, 2012-07-13 13:57:33

Here: D

if(Directory.Exists(@"C:\Windows\SysWOW64") == true)
{
  Console.WriteLine("Ваш виндовс, безусловно 64 разрядный.");
}
else 
{
  Console.WriteLine("Не, разрядность вашего виндовс, такая же, сколько у человека зубов =(");
}
Console.ReadLine();

A joke of course, but it rolls) If anything, you can change it to

 if (Directory.Exists(@"C:\Program Files (x86)") == true)
 {
    Console.WriteLine("Ваш виндовс, безусловно 64 разрядный.");
 }
 else 
 {
    Console.WriteLine("Не, разрядность вашего виндовс, такая же, сколько у человека зубов =(");
 }
 Console.ReadLine();
 2
Author: Vladimir Proskurin, 2012-07-13 14:01:30

Here is a good post about this:

 -2
Author: Чад, 2012-07-13 13:19:41
    class Program
    {
        static void Main(string[] args)
        {
            if (Environment.Is64BitOperatingSystem) Console.WriteLine("64Bit");
            else Console.WriteLine("32Bit");
            Console.ReadKey();
        }
    }
 -2
Author: knizera, 2012-07-13 15:42:06
bool is64bit = System.Environment.OSVersion.Major > 5;
 -4
Author: Anonym_ous, 2012-07-13 21:02:49