1

i am using Visual C# 2010 express and i need the most reliable way (on button click) and in .NET 2.0 framework to detect if windows is currently x86 or x64 in a message box.. up till now i have been using this code but i need to know if there is a more accurate way?

        string target = @"C:\Windows\SysWow64";
        {
            if (Directory.Exists(target))
            {
               MessageBox.Show("x64");
            }
            else
            {
               MessageBox.Show("x86");
            }
John Saunders
  • 160,644
  • 26
  • 247
  • 397
NightsEvil
  • 11
  • 1
  • 2

5 Answers5

9

By far the simplest test is to check the size of an IntPtr:

        if (IntPtr.Size == 8)
        {
           MessageBox.Show("x64");
        }
        else
        {
           MessageBox.Show("x86");
        }

Which assumes you build your EXE with the default Platform Target set to "Any CPU". Beware that this default changed in VS2010.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • 1
    IA-64 architecture have same size as x64 – ujeenator May 11 '16 at 12:55
  • The odds that your program runs on an Itanium multiplied by the odds of finding a .NET Framework version for it is sufficiently close to zero to consider this an irrelevant detail. – Hans Passant May 11 '16 at 13:11
  • Very relevant, when you developing .NET backend both for Windows Server x64 and Windows Server Itanium. – ujeenator May 11 '16 at 13:20
  • Click the Ask Question button if you can't figure out how to change a MessageBox.Show() call please. This question is only about the distinction between x86 and x64. – Hans Passant May 11 '16 at 13:24
  • Hans, I understand that this question was only about detection of x86 and x64 architectures, but stackoverflow is shared knowledge database. And when I searching for "c# detect architecture" this answer is in the top of search result. Someone can make a mistake and Copy-Paste your answer. But I personally understand that depending on pointer size is very bad solution, it can cause problem in many ways. So I marked your answer as bad. And I have no goal to arguing, my first comment was warning for other users, not claim on you. – ujeenator May 11 '16 at 13:40
  • Well, post your own answer and stop harassing me. – Hans Passant May 11 '16 at 13:45
7

You can use the PROCESSOR_ARCHITECTURE environment variable...

System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE")

It will return one of: x86, AMD64, IA64.

You are probably only interested in the x86 and AMD64 values, the IA64 is not very popular and not being supported by Microsoft in the future. It is for Itanium and Itanium 2 processors.

Another simple method is to look in the registry to see if SOFTWARE\Wow6432Node exists for HKLM or HKCU.

Brian R. Bondy
  • 339,232
  • 124
  • 596
  • 636
  • what about Windows 8 RT? I've tried to google for System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE") output on that OS, but haven't managed to find any reliable info. – Salaros Dec 08 '12 at 07:51
  • I thought this only gave CPU type. It's possible and common to run a 32bit OS on 64bit CPUs. – Joel Coehoorn Nov 21 '13 at 14:56
  • @Salaros http://stackoverflow.com/questions/13807142/output-of-environment-getenvironmentvariableprocessor-architecture-and-opera – ujeenator May 11 '16 at 15:03
6

I think the best way is to use: System.Environment.Is64BitOperatingSystem.

Ruchit Rami
  • 2,273
  • 4
  • 28
  • 53
4

Had this for a while. I believe it's .NET 2.0 compatible but I'm not totally sure. You're probably only interested in cases 0 and 9 (they're the most common anyways).

public static string GetCpuArch()
{
    ManagementScope scope = new ManagementScope();
    ObjectQuery query = new ObjectQuery("SELECT Architecture FROM Win32_Processor");
    ManagementObjectSearcher search = new ManagementObjectSearcher(scope, query);
    ManagementObjectCollection results = search.Get();

    ManagementObjectCollection.ManagementObjectEnumerator e = results.GetEnumerator();
    e.MoveNext();
    ushort arch = (ushort)e.Current["Architecture"];

    switch (arch)
    {
        case 0:
            return "x86";
        case 1:
            return "MIPS";
        case 2:
            return "Alpha";
        case 3:
            return "PowerPC";
        case 6:
            return "Itanium";
        case 9:
            return "x64";
        default:
            return "Unknown Architecture (WMI ID " + arch.ToString() + ")";
    }
}
Dave Kilian
  • 1,012
  • 8
  • 6
2

The PROCESSOR_ARCHITECTURE environment variable contains the address width of the running process, which is not necessarily that of the operating system or processors. A quick way to see this is by running the following command...

$Env:PROCESSOR_ARCHITECTURE

...in 32- and 64-bit PowerShell sessions and comparing the output.

So, if GetEnvironmentVariable("PROCESSOR_ARCHITECTURE") returns "AMD64" then you definitely have a 64-bit process, operating systems, and processors. If it returns "x86" then you definitely have a 32-bit process, though you still won't know if the operating system and processors are 32- or 64-bit.

If you're really after the address width of the operating system, then .NET 4 offers the Environment.Is64BitOperatingSystem property. You can also use WMI on any version of .NET to read the OSArchitecture property of the Win32_OperatingSystem class:

static string GetOSArchitecture()
{
    var query = new WqlObjectQuery("SELECT OSArchitecture FROM Win32_OperatingSystem");

    using (var searcher = new ManagementObjectSearcher(query))
    using (var results = searcher.Get())
    using (var enumerator = results.GetEnumerator())
    {
        enumerator.MoveNext();

        return (string) enumerator.Current.GetPropertyValue("OSArchitecture");
    }
}

...though, unfortunately, the OSArchitecture property only exists on Windows Vista/Server 2008 and above.

For all versions of Windows since 2000, you might try p/invoking the GetSystemInfo() function and checking the wProcessorArchitecture member of the SYSTEM_INFO structure.

Lance U. Matthews
  • 15,725
  • 6
  • 48
  • 68