views:

189

answers:

5
+5  Q: 

32/64 Bit Question

Here's my question. What is the best way to determine what bit architecture your app is running on?

What I am looking to do: On a 64 bit server I want my app to read 64 bit datasources (stored in reg key Software\Wow6432Node\ODBC\ODBC.INI\ODBC Data Sources) and if its 32 bit I want to read 32 bit datasources, (i.e. Read from Software\ODBC\ODBC.INI\ODBC Data Sources).

I might be missing the point, but I don't want to care what mode my app is running in. I simply want to know if the OS is 32 or 64 bit.

[System.Environment.OSVersion.Platform doesn't seem to be cutting it for me. Its returning Win32NT on my local xp machine and on a win2k8 64 bit server (even when all my projects are set to target 'any cpu')]

+3  A: 

You shouldn't even worry about this, normally. The system automatically redirects registry queries to Software\Wow6432Node when running a 32-bit app on a 64-bit platform.

JSBangs
To make your app run as 32-bit regardless of the platform, change the exe platform to "x86 only". It will still run on x64 platforms and be automatically redirected to the Wow6432Node keys.
Stephen Cleary
+4  A: 

Try the property Environment.Is64BitOperatingSystem. This is a new one added in .Net 4.0 specifically for the purpose of checking the type of the operating system.

JaredPar
Thanks but I should have mentioned I'm restricted to .net 2.0
+2  A: 

You should not read Wow6432Node directly. Use RegistryView to specify a 32-bit view when running as a 64-bit app.

Stephen Cleary
Unfortunately `RegistryView` available only from .NET 4.0.
Regent
+1  A: 

How to detect Windows 64 bit platform with .net? might be helpful.

Regent
I used this thanks. Thanks everyone else for their help also.
+4  A: 

Simple, safe, framework version agnostic solution without going to the registry:

Console.WriteLine(
    "Is 64-bit? {0}",
    (
        System.Runtime.InteropServices.Marshal.SizeOf(typeof(IntPtr)) == sizeof(Int64)
            ? "Yes" 
            : "No"
    )
);
Nathan Ernst
Pretty elegant!
code4life
Yes but if its running in 32 bit mode on a 64 bit machine the above would return false no?