vba 获取包含 32 位程序的“程序文件”文件夹的路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8701967/
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
Get path of "Program files" folder that contains 32-bit programs
提问by James
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)".
如何使用 VBA 确定包含 32 位程序的文件夹的完整路径?它在 32 位 Windows 系统上称为“Program Files”,但在 64 位系统上称为“Program Files (x86)”。
回答by ray
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")
List of environment variables can be found here.
可以在此处找到环境变量列表。
UPDATE: Based on the conversation I've had with Christian and based on my comments, I looked into this a little more.
更新:根据我与 Christian 的对话以及我的评论,我对此进行了更多研究。
I have two machines I tested on:
我有两台我测试过的机器:
- Machine 1: Win 7 Ultimate, 64 Bit, Office 2010 64 Bit
- Machine 2: Win 7 Ultimate, 32 Bit, Office 2007 32 Bit
- 机器 1:Win 7 Ultimate,64 位,Office 2010 64 位
- 机器 2:Win 7 Ultimate,32 位,Office 2007 32 位
I ran the following statements in the immediate window:
我在即时窗口中运行了以下语句:
? Environ("ProgramFiles")
? Environ("PROGRAMFILES(X86)")
? Environ("ProgramW6432")
Results
结果
Machine 1:
机器1:
C:\Program Files
C:\Program Files (x86)
C:\Program Files
Machine 2:
机器2:
C:\Program Files
//Blank//
//Blank//
So, based on these limited findings, you may want to see the if ProgramW6432has a value. If not, assume 32 bit and use ProgramFiles.
因此,基于这些有限的发现,您可能希望查看ProgramW6432是否具有值。如果不是,假设 32 位并使用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
Conversely, you could use PROCESSOR_IDENTIFIER to determine x64 vs. x86 and do the same thing.
相反,您可以使用 PROCESSOR_IDENTIFIER 来确定 x64 与 x86 并执行相同的操作。
I wouldn't say either way is foolproof but should get you on the right track.
我不会说任何一种方式都是万无一失的,但应该能让你走上正轨。
回答by Jean-Fran?ois Corbett
One-stop shop
一站式商店
I thought I'd summarize the conclusions that can be drawn from the information scattered across all answers here, plus a bit of integration on my part. Credits to all previous answer posters!
我想我会总结可以从分散在所有答案中的信息中得出的结论,再加上我的一些整合。归功于所有以前的答案海报!
The output of Environ
for any given "program file"-related environment variable will vary depending on Windows (32 or 64 bit) as well as Office (32 or 64 bit) as follows:
的输出Environ
对于任何给定“程序文件”相关的环境变量将取决于视窗(32或64位),以及办公室(32或64位)如下变化:
Windows Office ProgramFiles PROGRAMFILES(X86) ProgramW6432
------- ------ ---------------------- --------------------- ----------------
32-bit 32-bit C:\Program Files [empty string] [empty string]
64-bit 32-bit C:\Program Files (x86) C:\Program Files (x86) C:\Program Files
64-bit 64-bit C:\Program Files C:\Program Files (x86) C:\Program Files
Note that for the Windows 64 bit + Office 32 bit setup, the output of Environ("ProgramFiles")
does notmatch with the actual value of the ProgramFiles
environment variable in Windows! At the command prompt, echo %ProgramFiles%
returns C:\Program Files
, not C:\Program Files (x86)
.
请注意,在Windows 64位+ 32处设置位,输出Environ("ProgramFiles")
并没有匹配的实际值ProgramFiles
在Windows环境变量!在命令提示符下,echo %ProgramFiles%
返回C:\Program Files
,而不是C:\Program Files (x86)
。
If you need the path to the 32 bit program files folder (C:\Program Files
for 32-bit Windows, and C:\Program Files (x86)
for 64-bit Windows) then you can use this function:
如果您需要 32 位程序文件文件夹的路径(C:\Program Files
对于 32 位 Windows 和C:\Program Files (x86)
64 位 Windows),那么您可以使用此功能:
Function Get32BitProgramFilesPath() As String
If Environ("ProgramW6432") = "" Then
'32 bit Windows
Get32BitProgramFilesPath = Environ("ProgramFiles")
Else
'64 bit Windows
Get32BitProgramFilesPath = Environ("ProgramFiles(x86)")
End If
End Function
回答by Christian Specht
ray023's answeris basically correct, but one addition:
ray023 的回答基本正确,但补充一点:
At least on my machine (Win 7 Home Premium 64 bit, Access 2000 installed), bothEnviron("ProgramFiles")
andEnviron("PROGRAMFILES(X86)")
至少在我的机器上(Win 7 Home Premium 64 位,安装了 Access 2000),Environ("ProgramFiles")
和Environ("PROGRAMFILES(X86)")
...return the same folder, C:\Program Files (x86)
.
...返回相同的文件夹,C:\Program Files (x86)
。
To get the "non-x86-folder" (C:\Program Files
) on my 64 bit Windows, I need to use Environ("ProgramW6432")
.
要C:\Program Files
在 64 位 Windows 上获取“非 x86 文件夹”( ),我需要使用Environ("ProgramW6432")
.
Here's another linkabout the Environ
function, including code how to list all environment variables (that's how I found ProgramW6432
).
这是关于该Environ
函数的另一个链接,包括如何列出所有环境变量的代码(这就是我发现的方式ProgramW6432
)。
EDIT:
编辑:
As I already said in a comment, I just tested it on my other machine as the results seem to depend not only on the operating system, but on the installed MS Office version as well:
正如我在评论中已经说过的,我只是在我的另一台机器上测试了它,因为结果似乎不仅取决于操作系统,还取决于安装的 MS Office 版本:
This machine runs on Win XP SP3 32-bit, and Access 2000 is installed:
本机运行Win XP SP3 32位,安装Access 2000:
Environ("ProgramFiles")
returns C:\Programme
.
(that's "Program Files" in German - I'm in Germany and my Windows is in German)
Environ("ProgramFiles")
返回C:\Programme
。
(这是德语中的“程序文件” - 我在德国,我的 Windows 是德语)
Environ("PROGRAMFILES(X86)")
and Environ("ProgramW6432")
return an empty string.
Environ("PROGRAMFILES(X86)")
并Environ("ProgramW6432")
返回一个空字符串。
--> So the safest way to determine the "x86 folder" (no matter if on Win XP or Win 7) seems to be Environ("ProgramFiles")
.
--> 因此,确定“x86 文件夹”(无论是在 Win XP 还是 Win 7 上)的最安全方法似乎是Environ("ProgramFiles")
.
回答by Doc Brown
Here is an article showing you how to read this information from the registry:
这是一篇文章,向您展示如何从注册表中读取此信息:
In the comments of that article is also a hint how to retrieve the information from the environment variable "ProgramFiles". But beware, if you have different partitions, there may be more than one "Program Files" folder, for example "C:\Program Files"
and "D:\Program Files"
.
在那篇文章的评论中还提示了如何从环境变量“ProgramFiles”中检索信息。但请注意,如果您有不同的分区,则可能会有多个“Program Files”文件夹,例如"C:\Program Files"
和"D:\Program Files"
。