En  Fa
Discovering: Articles  > Platforms & Libraries  > .NET Framework  > How to detect 32bit or 64bit OS
 
 

How to detect 32bit or 64bit OS

   

How to detect 32bit or 64bit OS

Using IntPtr

IntPtr used to keep native handles in dotNET. Because of its structure it is important to what size it is. This is a our trick, checking size of IntPtr. IntPtr will be 8 if application is running on a 64bit OS, otherwise in 32bit OS it will be 4.

C# Code

if(IntPtr.Size == 8) 
{
	// 64 bit machine
} 
else if(IntPtr.Size == 4) 
{
	// 32 bit machine
}

VB.NET Code

If IntPtr.Size = 8 Then

	' 64 bit machine

ElseIf IntPtr.Size = 4 Then

	' 32 bit machine

End If

Other methods

Another way is checking for the existence of the "ProgramFiles(x86)" environment variable. This is not a recomended way but anyway you can use it!

public bool Is64bitOS
{
    get { return (Environment.GetEnvironmentVariable("ProgramFiles(x86)") != null); }
}

public string ProgramFilesX86
{
    get
    {
        string programFiles = Environment.GetEnvironmentVariable("ProgramFiles(x86)");
        if (programFiles == null)
        {
            programFiles = Environment.GetEnvironmentVariable("ProgramFiles");
        }

        return programFiles;
    }
}

The Is64bitOS property will be true if application is ruunig on a 64bit OS.

Another way is GetNativeSystemInfo windows API. Because it is an API and needs PInvode it is not recommended but it is a reliable way.

Details
      
Writer: Salar Khalilzadeh
Date Sent: 3/28/2009 11:34 AM
Views: 1972
Votes: 4
Rating:   from 3.50 Points

Your Rating:

bookmark this
 

There is no comment for this topic.
Language:

Copyright © 2009 SoftProjects.org | About | Valid XHTML | CSS