如何使用VBScript确定我是运行32位还是64位Windows操作系统?

San*_*osh 12 windows vbscript 32bit-64bit

如何在VBScript中检测Windows操作系统的位数(32位与64位)?

我试过这种方法,但它不起作用; 我猜(x86)导致检查文件夹的一些问题..

还有其他选择吗?

progFiles="c:\program files" & "(" & "x86" & ")"

set fileSys=CreateObject("Scripting.FileSystemObject")

If fileSys.FolderExists(progFiles) Then    
   WScript.Echo "Folder Exists"    
End If
Run Code Online (Sandbox Code Playgroud)

Bru*_*uno 19

前几天在工作中遇到了同样的问题.偶然发现了这个天才的vbscript片段,并认为不分享是太好了.

Bits = GetObject("winmgmts:root\cimv2:Win32_Processor='cpu0'").AddressWidth
Run Code Online (Sandbox Code Playgroud)

资料来源:http://csi-windows.com/toolkit/csi-getosbits

  • +1.49 - 很好的解决方案 - 但是 - 唉 - 适用于WMI时间(用茶杯测量). (3认同)

Dir*_*mar 15

你可以查询PROCESSOR_ARCHITECTURE.这里描述的,你必须添加一些额外的检查,因为它的值PROCESSOR_ARCHITECTUREx86适用于任何32位进程,即使它在64位操作系统上运行.在这种情况下,变量PROCESSOR_ARCHITEW6432将包含OS位数.MSDN中的更多细节.

Dim WshShell
Dim WshProcEnv
Dim system_architecture
Dim process_architecture

Set WshShell =  CreateObject("WScript.Shell")
Set WshProcEnv = WshShell.Environment("Process")

process_architecture= WshProcEnv("PROCESSOR_ARCHITECTURE") 

If process_architecture = "x86" Then    
    system_architecture= WshProcEnv("PROCESSOR_ARCHITEW6432")

    If system_architecture = ""  Then    
        system_architecture = "x86"
    End if    
Else    
    system_architecture = process_architecture    
End If

WScript.Echo "Running as a " & process_architecture & " process on a " _ 
    & system_architecture & " system."
Run Code Online (Sandbox Code Playgroud)


Dav*_*dRR 5

Here is a pair of VBScript functions based on the very concise answer by @Bruno:

Function Is32BitOS()
    If GetObject("winmgmts:root\cimv2:Win32_Processor='cpu0'").AddressWidth _
       = 32 Then
        Is32BitOS = True
    Else
        Is32BitOS = False
    End If
End Function

Function Is64BitOS()
    If GetObject("winmgmts:root\cimv2:Win32_Processor='cpu0'").AddressWidth _
       = 64 Then
        Is64BitOS = True
    Else
        Is64BitOS = False
    End If
End Function
Run Code Online (Sandbox Code Playgroud)

UPDATE: Per the advice from @Ekkehard.Horner, these two functions can be written more succinctly using single-line syntax as follows:

Function Is32BitOS() : Is32BitOS = (GetObject("winmgmts:root\cimv2:Win32_Processor='cpu0'").AddressWidth = 32) : End Function

Function Is64BitOS() : Is64BitOS = (GetObject("winmgmts:root\cimv2:Win32_Processor='cpu0'").AddressWidth = 64) : End Function
Run Code Online (Sandbox Code Playgroud)

(Note that the parentheses that surround the GetObject(...) = 32 condition are not necessary, but I believe they add clarity regarding operator precedence. Also note that the single-line syntax used in the revised implementations avoids the use of the If/Then construct!)

UPDATE 2: Per the additional feedback from @Ekkehard.Horner, some may find that these further revised implementations offer both conciseness and enhanced readability:

Function Is32BitOS()
    Const Path = "winmgmts:root\cimv2:Win32_Processor='cpu0'"
    Is32BitOS = (GetObject(Path).AddressWidth = 32)
End Function

Function Is64BitOS()
    Const Path = "winmgmts:root\cimv2:Win32_Processor='cpu0'"
    Is64BitOS = (GetObject(Path).AddressWidth = 64)
End Function
Run Code Online (Sandbox Code Playgroud)


Bur*_*kan 5

WMIC 查询可能很慢。使用环境字符串:

Function GetOsBits()
   Set shell = CreateObject("WScript.Shell")
   If shell.ExpandEnvironmentStrings("%PROCESSOR_ARCHITECTURE%") = "AMD64" Then
      GetOsBits = 64
   Else
      GetOsBits = 32
   End If
End Function
Run Code Online (Sandbox Code Playgroud)