Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
How to get the details of OS, wehther it is win XP, WIn 7, whether 32 bit ot 64 bit, etc from .net 2008?
Posted
Comments
Sergey Alexandrovich Kryukov 2-Mar-12 1:12am    
For goodness sake, what is ".NET 2008"? There is no such thing?
--SA

You can find all the information from System.Environment:

C#
System.OperatingSystem system = System.Environment.OSVersion;


All the version information, and the version information on service packs — all is contained in this stricture. Except one detail: it does not explicitly say if it is 32- or 64-bit. Probably this is because this matter is from the different field: CPU instruction-set architecture.

One simple way to get and recognize this string:

C#
System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE")


Another way to figure out if you are running 32-bit or 64-bit code is to find a size of IntPtr:

C#
int bits = System.Runtime.InteropServices.Marshal.SizeOf(typeof(System.IntPtr)) * 8;


This call will return either 32 or 64. You should understand that is says nothing about real CPU architecture, it says only the architecture on which the code is currently running. You can set a 32-bit target architecture for the application, and it will run as 32-bit (x86) even on a 64-bit system, on top of WoW64.

Please see:
http://en.wikipedia.org/wiki/WoW64[^].

See also on the instruction-set architectures:
http://en.wikipedia.org/wiki/Instruction_set_architecture[^].

Supported targets by .NET:
http://en.wikipedia.org/wiki/X86[^],
http://en.wikipedia.org/wiki/X86-64[^],
http://en.wikipedia.org/wiki/Itanium[^].

Note that Itanium (IA-64) and x86-I64 are incompatible, but both are compatible with 32-bit x86.

—SA
 
Share this answer
 
v5
Below provided function will return the x86 Program Files directory in all 3 of the windows configurations

32 bit Windows
32 bit program running on 64 bit Windows
64 bit program running on 64 bit windows

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

     return Environment.GetEnvironmentVariable("ProgramFiles");
}
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900