Reputation: 4997
Is it possible to check if computer is 32 bit or 64 using vb.net code? I just want to display the result in a message.
Please advise.
Upvotes: 20
Views: 40530
Reputation: 83
You can easily use this function:
Function Is64BitOS() As Boolean
If IO.Directory.Exists(IO.Path.GetDirectoryName(Environment.GetFolderPath(Environment.SpecialFolder.System)) + IO.Path.DirectorySeparatorChar + "SysWOW64") Then
Return True '64bit
Else
Return False '32bit
End If
End Function
Example usage:
If Is64BitOS() Then
MsgBox("Is 64bit OS")
Else
MsgBox("Is 32bit OS")
End If
It should work on all vb.net versions. Tested on Visual Basic 2010 .NET Framework 2.0
Upvotes: 0
Reputation: 1
when u need .net 2 (to work around on all windows versions) it has a very simple way to do it
Function isit64() As Integer
If Directory.Exists("c:\windows\syswow64") And Directory.Exists("c:\program files (x86)") Then
Return 1
Else
Return 0
End If
End Function
Upvotes: 0
Reputation: 2812
Use
If System.IO.Directory.Exists("C:\Program Files (x86)") Then
MsgBox("64-Bit OS")
Else
MsgBox("32-Bit OS")
End If
It will work on all the framework versions
Upvotes: -2
Reputation: 21
VB.NET: What I wanted works as below. Define the custom constant Win64
in x64 all configurations (debug, release etc), like in the diagram below, and use it as follows:
If (Win64) Then
'64 bit code
else
' 32 bit code here
End If
Upvotes: 0
Reputation: 43
Msgbox (Runtime.InteropServices.Marshal.SizeOf(GetType(IntPtr)) * 8)
Upvotes: -1
Reputation: 11
I simply use this piece of code and it works fine:
If System.Environment.Is64BitOperatingSystem = True Then
MessageBox.Show("OS System : 64 Bit Operating System")
Else
MessageBox.Show("OS System : 32 Bit Operating System")
End If
Upvotes: 1
Reputation: 27
If IntPtr.Size = 8 Then
' 64 bit machine
ElseIf IntPtr.Size = 4 Then
' 32 bit machine
End If
Upvotes: 0
Reputation: 20565
IntPtr.Size won't return the correct value if running in 32-bit .NET Framework 2.0 on 64-bit Windows (it would return 32-bit).
You have to first check if running in a 64-bit process (I think in .NET you can do so by checking IntPtr.Size), and if you are running in a 32-bit process, you still have to call the Win API function IsWow64Process. If this returns true, you are running in a 32-bit process on 64-bit Windows.
Microsoft's Raymond Chen: How to detect programmatically whether you are running on 64-bit Windows
Solution:
Private is64BitProcess As Boolean = (IntPtr.Size = 8)
Private is64BitOperatingSystem As Boolean = is64BitProcess OrElse InternalCheckIsWow64()
<DllImport("Kernel32.dll", SetLastError:=True, CallingConvention:=CallingConvention.Winapi)> _
Public Shared Function IsWow64Process( _
ByVal hProcess As Microsoft.Win32.SafeHandles.SafeHandleZeroOrMinusOneIsInvalid, _
ByRef wow64Process As Boolean) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
Public Shared Function InternalCheckIsWow64() As Boolean
If (Environment.OSVersion.Version.Major = 5 AndAlso Environment.OSVersion.Version.Minor >= 1) OrElse Environment.OSVersion.Version.Major >= 6 Then
Using p As Process = Process.GetCurrentProcess()
Dim retVal As Boolean
If Not IsWow64Process(p.Handle, retVal) Then
Return False
End If
Return retVal
End Using
Else
Return False
End If
End Function
Upvotes: 1
Reputation: 498904
Environment.Is64BitOperatingSystem
should do nicely.
Determines whether the current operating system is a 64-bit operating system.
The assumption being that a false signifies a 32bit environment.
If you want to find out if the process is 64bit (as you can run a 32bit process on a 64bit OS), use Environment.Is64BitProcess:
Determines whether the current process is a 64-bit process.
Both of these have been introduced in .NET 4.0.
Upvotes: 36