获取“程序文件"的路径包含 32 位程序的文件夹 [英] Get path of "Program files" folder that contains 32-bit programs
问题描述
如何使用 VBA 确定包含 32 位程序的文件夹的完整路径?它在 32 位 Windows 系统上称为Program Files",但在 64 位系统上称为Program Files (x86)".
How do I determine the full path of the folder that contains 32-bit programs using VBA? It's called "Program Files" on 32-bit Windows systems, but on 64-bit systems it's called "Program Files (x86)".
推荐答案
Environ 会解决问题:
Environ will do the trick:
debug.print Environ("ProgramFiles")
debug.print Environ("PROGRAMFILES(X86)")
'If you want to check if current PC is x64
debug.print Environ("PROCESSOR_IDENTIFIER")
可以在此处找到环境变量列表.
更新:根据我与 Christian 的对话以及我的评论,我对此进行了更多研究.
UPDATE: Based on the conversation I've had with Christian and based on my comments, I looked into this a little more.
我测试了两台机器:
- 机器 1:Win 7 Ultimate、64 位、Office 2010 64 位
- 机器 2:Win 7 Ultimate,32 位,Office 2007 32 位
- Machine 1: Win 7 Ultimate, 64 Bit, Office 2010 64 Bit
- Machine 2: Win 7 Ultimate, 32 Bit, Office 2007 32 Bit
我在即时窗口中运行了以下语句:
I ran the following statements in the immediate window:
? Environ("ProgramFiles")
? Environ("PROGRAMFILES(X86)")
? Environ("ProgramW6432")
结果
机器 1:
C:\Program Files
C:\Program Files (x86)
C:\Program Files
机器 2:
C:\Program Files
//Blank//
//Blank//
因此,根据这些有限的发现,您可能希望查看 ProgramW6432 是否具有值.如果不是,假设 32 位并使用 ProgramFiles.
So, based on these limited findings, you may want to see the if ProgramW6432 has a value. If not, assume 32 bit and use ProgramFiles.
IF Environ("ProgramW6432") <> "" THEN
'I'm 64 bit so check both ProgramW6432 and PROGRAMFILES(X86)
ELSE
'I'm 32 bit so check ProgramFiles
END IF
相反,您可以使用 PROCESSOR_IDENTIFIER 来确定 x64 与 x86 并执行相同的操作.
Conversely, you could use PROCESSOR_IDENTIFIER to determine x64 vs. x86 and do the same thing.
我不会说任何一种方法都是万无一失的,但应该能让你走上正轨.
I wouldn't say either way is foolproof but should get you on the right track.
这篇关于获取“程序文件"的路径包含 32 位程序的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!