0

I'm interfacing with an Access database and so have to use either of two connection strings (Jet or Ace) depending on my platform selection (i.e. x86 or x64). The code that I'm working on could be compiled with either platform so I need a way to detect the platform "bitness". I've been told I can use IntPtr but I'm not sure how.

Thanks.

Edits:

Just want to say, this is NOT a duplicate! In the suggested link, the OP is asking for the Operating system's bitness, not the process's. And they are talking about .NET 2.0. I haven't seen anything in 2.0 in like three years.

Jordan
  • 9,642
  • 10
  • 71
  • 141

3 Answers3

3

You can use Environment.Is64BitProcess to determine if the current process is a 64 or 32 bit process.

Servy
  • 202,030
  • 26
  • 332
  • 449
  • 1
    Yes, and if .NET 3.5 or older is being targeted (which doesn't have this property), you can use `IntPtr.Size == 8` to detect if it is x64, and size will be 4 for x86. – vcsjones Oct 15 '13 at 19:00
  • I'm using .NET 4.5, and `Environment.Is64BitProcess` does exist. I prefer it because it better explains what I'm doing. – Jordan Oct 15 '13 at 19:10
2

The System.Environment.Is64BitProcess property gives you that information.

Ondrej Tucny
  • 27,626
  • 6
  • 70
  • 90
1

Check the answer to this question:

.NET 2.0 Framework in C# check if 64bit os if so do this? if not do this? better answer?

It basically checks if IntPtr.Size returns 4 or 8 byte number.

Community
  • 1
  • 1
Szymon
  • 42,577
  • 16
  • 96
  • 114
  • `IntPtr` doesn't return anything. Its a type. Do you mean `IntPtr.Size`? – Jordan Oct 15 '13 at 19:11
  • @Jordan Thanks for pointing out, I corrected the answer. – Szymon Oct 15 '13 at 19:12
  • I saw that question before posting this. It didn't find it satisfactory. I try to avoid using API calls. `Environment.Is64BitProcess` works fine for me. And it's about .NET 2.0 so it would be good to have an updated question for people to find. – Jordan Oct 15 '13 at 19:14
  • Also, the OP there is detecting the OS bitness, not the process. – Jordan Oct 15 '13 at 19:16