如何使用 VBScript 确定我运行的是 32 位还是 64 位 Windows 操作系统?
如何在 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
How do i detect the bitness (32-bit vs. 64-bit) of the Windows OS in VBScript?
I tried this approach but it doesn't work; I guess the (x86) is causing some problem which checking for the folder..
Is there any other alternative?
progFiles="c:\program files" & "(" & "x86" & ")"
set fileSys=CreateObject("Scripting.FileSystemObject")
If fileSys.FolderExists(progFiles) Then
WScript.Echo "Folder Exists"
End If
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
前几天在工作中也遇到了同样的问题。偶然发现了这个天才的 vbscript 作品,觉得它太好了,不能不分享。
来源:http://csi-windows.com/toolkit/csi-getosbits
Came up against this same problem at work the other day. Stumbled on this genius piece of vbscript and thought it was too good not to share.
Source: http://csi-windows.com/toolkit/csi-getosbits
您可以查询
PROCESSOR_ARCHITECTURE
。 此处进行了描述,您必须添加一些额外的检查,因为对于任何 32 位进程,PROCESSOR_ARCHITECTURE
的值都将为x86
,即使它运行在 64 位上操作系统。在这种情况下,变量PROCESSOR_ARCHITEW6432
将包含操作系统位数。更多详细信息请参见 MSDN。You can query the
PROCESSOR_ARCHITECTURE
. A described here, you have to add some extra checks, because the value ofPROCESSOR_ARCHITECTURE
will bex86
for any 32-bit process, even if it is running on a 64-bit OS. In that case, the variablePROCESSOR_ARCHITEW6432
will contain the OS bitness. Further details in MSDN.这是基于 @Bruno 非常简洁的答案的一对 VBScript 函数:
更新: 根据 @Ekkehard.Horner 的建议,这两个函数可以写得更简洁 使用单行语法,如下所示:
(请注意
周围的括号GetObject(...) = 32 条件不是必需的,但我相信它们增加了有关运算符优先级的清晰度。另请注意,修订后的实现中使用的单行语法避免了使用 If/。然后
构造!)更新2:根据@Ekkehard.Horner的额外反馈,有些人可能会发现这些进一步修改的实现既简洁又增强了可读性:
Here is a pair of VBScript functions based on the very concise answer by @Bruno:
UPDATE: Per the advice from @Ekkehard.Horner, these two functions can be written more succinctly using single-line syntax as follows:
(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 theIf/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:
WMIC 查询可能会很慢。使用环境字符串:
WMIC queries may be slow. Use the environment strings:
确定CPU是32位还是64位很容易,但问题是如何确定操作系统是32位还是64位。当 64 位 Windows 运行时,ProgramW6432 环境变量被定义。
这:
对于 32 位操作系统将返回 true,对于 64 位操作系统将返回 false,并且适用于所有版本的 Windows,包括非常旧的版本。
Determining if the CPU is 32-bit or 64-bit is easy but the question asked is how to determine if the OS is 32-bit or 64-bit. When a 64-bit Windows is running, the ProgramW6432 environment variable is defined.
This:
will return true for a 32-bit OS and false for a 64-bit OS and will work for all version of Windows including very old ones.
Bruno 答案的附录:您可能想要检查操作系统而不是处理器本身,因为您可以在较新的 CPU 上安装较旧的操作系统:
返回字符串“32 位”或“64 位”。
Addendum to Bruno's answer: You may want to check the OS rather than the processor itself, since you could install an older OS on a newer CPU:
Returns string "32-bit" or "64-bit".
您还可以检查文件夹
C:\Windows\sysnative
是否存在。此文件夹(或更好的别名)仅存在于 32 位进程中,请参阅 文件系统重定向器注意:此脚本显示您当前的进程是在 32 位模式还是 64 位模式下运行 - 它不显示您的 Windows 体系结构。
You can also check if folder
C:\Windows\sysnative
exist. This folder (or better alias) exist only in 32-Bit process, see File System RedirectorNote: this script shows whether your current process is running in 32-bit or 64-bit mode - it does not show the architecture of your Windows.
使用环境。在XP中测试,但我找不到32位CPU来测试......
Using environment. Tested in XP, but I can't find a 32 bit CPU to test...
这与 Microsoft 博客 https://learn.microsoft.com/en-us/archive/blogs/david.wang/howto-detect-process-bitness
在 XP 32 和 win7 64 中测试(使用 32 位和 64 位 CMD )
This is the same proposed solution in Microsoft blog https://learn.microsoft.com/en-us/archive/blogs/david.wang/howto-detect-process-bitness
tested in XP 32 and win7 64 (using a 32 bit and 64 bit CMD)