13

Using C#, I would like to create a method that retunrs whether my machine is 64 or 32-bit.

Is there anybody who knows how to do that?

FishBasketGordo
  • 22,904
  • 4
  • 58
  • 91
Slt
  • 179
  • 1
  • 1
  • 4
  • 2
    possible duplicate of [How can I programmatically determine my processor type?](http://stackoverflow.com/questions/1020581/how-can-i-programmatically-determine-my-processor-type) –  Aug 11 '11 at 19:50
  • @MyrS -There are different ways to do this depending on which .Net version is being used. Is64BitOperatingSystem() is not available with earlier .Net platform versions. – Jon Raynor Aug 11 '11 at 20:05

7 Answers7

24

System.Environment.Is64BitOperatingSystem

JGWeissman
  • 728
  • 6
  • 19
Vlad
  • 35,022
  • 6
  • 77
  • 199
6
System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE")

see this question.

Community
  • 1
  • 1
djhaskin987
  • 9,741
  • 4
  • 50
  • 86
4

Here it is:

How to detect Windows 64-bit platform with .NET?

Quote:

bool is64BitProcess = (IntPtr.Size == 8);
bool is64BitOperatingSystem = is64BitProcess || InternalCheckIsWow64();

[DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool IsWow64Process(
    [In] IntPtr hProcess,
    [Out] out bool wow64Process
);

public static bool InternalCheckIsWow64()
{
    if ((Environment.OSVersion.Version.Major == 5 && Environment.OSVersion.Version.Minor >= 1) ||
        Environment.OSVersion.Version.Major >= 6)
    {
        using (Process p = Process.GetCurrentProcess())
        {
            bool retVal;
            if (!IsWow64Process(p.Handle, out retVal))
            {
                return false;
            }
            return retVal;
        }
    }
    else
    {
        return false;
    }
}
Community
  • 1
  • 1
GrandMarquis
  • 1,913
  • 1
  • 18
  • 30
  • Upvoted, just because it's an incredibly complicated way to do an incredibly easy thing, and I think that's awesome. (That is, as long as it never appears in any of my production code.) `System.Environment.Is64BitOperatingSystem` will do the same thing in one line. – Jarrett Meyer Aug 11 '11 at 19:54
  • 1
    Why make it easy when you can complicate it? – GrandMarquis Aug 11 '11 at 19:58
  • 2
    @Jarrett - Not everyone has the luxury of doing it with later version(s) of .Net, so sometimes you have go to PInvoking. – Jon Raynor Aug 11 '11 at 20:01
  • Thanks! Is64BitOperatingSystem() works fine for me :) I guess i have the latest .Net platform version. – Slt Aug 11 '11 at 20:16
1
    public static string t2or64()
        {
            string t2s4;
            bool os = System.Environment.Is64BitOperatingSystem;
            int x = 0;
            if (os == true)
            {
                x = 64;
            }
            else
            {
                x = 32;
            }

            t2s4 = Convert.ToString(x);
            return t2s4;
        }
Nitin Patil
  • 110
  • 10
user3410566
  • 45
  • 2
  • 15
1

I have this coded for one of my projects (C# VS 2005).

//DLL Imports
using System.Runtime.InteropServices;            

            /// <summary>
            /// The function determines whether the current operating system is a 
            /// 64-bit operating system.
            /// </summary>
            /// <returns>
            /// The function returns true if the operating system is 64-bit; 
            /// otherwise, it returns false.
            /// </returns>
            public static bool Is64BitOperatingSystem()
            {
                if (IntPtr.Size == 8)  // 64-bit programs run only on Win64
                {
                    return true;
                }
                else  // 32-bit programs run on both 32-bit and 64-bit Windows
                {
                    // Detect whether the current process is a 32-bit process 
                    // running on a 64-bit system.
                    bool flag;
                    return ((DoesWin32MethodExist("kernel32.dll", "IsWow64Process") &&
                        IsWow64Process(GetCurrentProcess(), out flag)) && flag);
                }
            }



    /// <summary>
    /// The function determins whether a method exists in the export 
    /// table of a certain module.
    /// </summary>
    /// <param name="moduleName">The name of the module</param>
    /// <param name="methodName">The name of the method</param>
    /// <returns>
    /// The function returns true if the method specified by methodName 
    /// exists in the export table of the module specified by moduleName.
    /// </returns>
    static bool DoesWin32MethodExist(string moduleName, string methodName)
    {
        IntPtr moduleHandle = GetModuleHandle(moduleName);
        if (moduleHandle == IntPtr.Zero)
        {
            return false;
        }
        return (GetProcAddress(moduleHandle, methodName) != IntPtr.Zero);
    }

    [DllImport("kernel32.dll")]
    static extern IntPtr GetCurrentProcess();

    [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
    static extern IntPtr GetModuleHandle(string moduleName);

    [DllImport("kernel32", CharSet = CharSet.Auto, SetLastError = true)]
    static extern IntPtr GetProcAddress(IntPtr hModule,
        [MarshalAs(UnmanagedType.LPStr)]string procName);

    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool IsWow64Process(IntPtr hProcess, out bool wow64Process);
Jon Raynor
  • 3,804
  • 6
  • 29
  • 43
0

You can check using IntPtr size. IntPtr size is 4 for 32 bit OS and 8 for 64 bit OS

/// <summary>Is64s the bit operating system.</summary>
/// <returns></returns>
if (IntPtr.Size == 8)
    // 64Bit
else
    // 32bit

Type: System.Int32

The size of a pointer or handle in this process, measured in bytes. The value of this property is 4 in a 32-bit process, and 8 in a 64-bit process. You can define the process type by setting the /platform switch when you compile your code with the C# and Visual Basic compilers.

eigenharsha
  • 2,051
  • 1
  • 23
  • 32
0

You can use:

System.Runtime.InteropServices.RuntimeInformation.OSArchitecture

which returns an enum. Docs

Tareq Imbasher
  • 287
  • 2
  • 11