vb.net 如何检查操作系统是 32 位操作系统还是 64 位
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14423057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if OS is 32 Bit OS or 64 Bit
提问by Furqan Sehgal
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.
是否可以使用 vb.net 代码检查计算机是 32 位还是 64 位?我只想在消息中显示结果。
Please advise.
请指教。
回答by Oded
Environment.Is64BitOperatingSystem
should do nicely.
Environment.Is64BitOperatingSystem
应该做得很好。
Determines whether the current operating system is a 64-bit operating system.
确定当前操作系统是否为 64 位操作系统。
The assumption being that a false signifies a 32bit environment.
假设是 false 表示 32 位环境。
If you want to find out if the processis 64bit (as you can run a 32bit process on a 64bit OS), use Environment.Is64BitProcess:
如果您想确定进程是否为 64 位(因为您可以在 64 位操作系统上运行 32 位进程),请使用Environment.Is64BitProcess:
Determines whether the current process is a 64-bit process.
确定当前进程是否为 64 位进程。
Both of these have been introduced in .NET 4.0.
这两者都已在 .NET 4.0 中引入。
回答by Parimal Raj
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).
如果在 64 位 Windows 上的 32 位 .NET Framework 2.0 中运行,IntPtr.Size 将不会返回正确的值(它将返回 32 位)。
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.
你必须先检查是否在 64 位进程中运行(我认为在 .NET 中你可以通过检查 IntPtr.Size 来做到这一点),如果你在 32 位进程中运行,你仍然需要调用 Win API函数 IsWow64Process。如果返回 true,则您正在 64 位 Windows 上的 32 位进程中运行。
Microsoft's Raymond Chen: How to detect programmatically whether you are running on 64-bit Windows
微软的 Raymond Chen: 如何以编程方式检测您是否在 64 位 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
回答by user3348284
If IntPtr.Size = 8 Then
' 64 bit machine
ElseIf IntPtr.Size = 4 Then
' 32 bit machine
End If
回答by Rangga Stephen
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
回答by Muhammad Arshad Awan
回答by Hariprasath Yadav
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
它将适用于所有框架版本
回答by Andrei
Msgbox (Runtime.InteropServices.Marshal.SizeOf(GetType(IntPtr)) * 8)