-1

Possible Duplicate:
C# - How to get Program Files (x86) on Vista x64

What is the best way to handle navigating whether a computer is 64 bit or not in C#? Currently I do the following in C#:

bool is64bit = false;

Within the main method:

if (Directory.Exists("C:\\Program Files (x86)"))
{
    is64bit = true;
}

Is there a better way to handle Program Files then that? As in later when I call a process that installs by default in Program Files, is there a shortcut I can use other then seeing if it exists?

Community
  • 1
  • 1
Falcon165o
  • 2,875
  • 1
  • 20
  • 31

3 Answers3

2

Just use the %ProgramFiles% Environment Variable.

If running on a 64-bit system, depending on if you're application is running in 32-bit or 64-bit mode, it will appropriately return either C:\Program Files (x86) or C:\Program Files (assuming the user hasn't changed the location).

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
  • I've been looking for something like that before, my question is can I use that to compile file paths also? – Falcon165o Feb 22 '12 at 14:47
  • @Falcon165o - You could...I would just use Environment.GetFolderPath( Environment.SpecialFolder.CommonProgramFiles ) if I were you. – Security Hound Feb 22 '12 at 15:10
0

Check out this link: http://msdn.microsoft.com/en-us/library/system.environment.aspx

You can determine whether the OS is 64 bit or not programmatically with this line of code:

bool Is64Bit = System.Environment.Is64BitOperatingSystem;
Matt T
  • 511
  • 2
  • 8
  • You are just replacing one bool variable with another - rather pointless. Just use System.Environment.Is64BitOperatingSystem. – Polyfun Feb 22 '12 at 15:26
  • I think he is demonstrating the usage of the property. Nothing wrong with that. – Adam Feb 05 '14 at 04:04
-1

Try this:

public static bool IsX64()
        {
            if (8 == IntPtr.Size
                || (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432"))))
            {
                return true;
            }
            return false;
        }

Which can also be used specifically like:

public static string ProgramFilesx86()
        {
            if (8 == IntPtr.Size
                || (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432"))))
            {
                return Environment.GetEnvironmentVariable("ProgramFiles(x86)");
            }

            return Environment.GetEnvironmentVariable("ProgramFiles");
        }
Grant H.
  • 3,689
  • 2
  • 35
  • 53
  • Okay my question is if you use that; it looks like you are checking the Processor. Is this still correct if you have a 64 bit processor with a x86 installed OS? I used something *kind of* like this before and i got burned on that aspect. – Falcon165o Feb 22 '12 at 14:51
  • I believe that that environment variable will only be set when running a 32 bit process in WOW64 mode. So between that and the pointer check, you should have the bases covered correctly. Also note that that variable is different from `PROCESSOR_ARCHITECTURE` which wouldn't work, and would present the issue you described. – Grant H. Feb 22 '12 at 15:02
  • This has to be the worst suggestion I have ever seen. There is no reason to do any of this. The enumeration Environment.SpecialFolder contained values for both Program Files and Program FilesX86 – Security Hound Feb 22 '12 at 15:09
  • Worst suggestion you've ever seen? It works without error. There are obviously plenty of ways to do things...I see no issue with this one. It also accounts for the ambiguity of the OP's question, as to whether he was seeking the correct program files folder, or using that to detect bitness. – Grant H. Feb 22 '12 at 15:12
  • 2
    Let's keep comments constructive, please. – Tim Post Feb 22 '12 at 17:51