vba 使用 VBS 确定程序文件的位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7555780/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 14:06:33  来源:igfitidea点击:

Determining the location of Program Files using VBS

vbavbscriptdirectoryspecial-folders

提问by Carlos

What would a safe way of extracting the 'Program Files' directory location using VBS?. I would like to get the directory location from the environment as it will avoid localization issues as well as problems between different OS architectures (32/64 bit) and drive (C:\, D:\, etc:).

使用 VBS 提取“程序文件”目录位置的安全方法是什么?我想从环境中获取目录位置,因为它可以避免本地化问题以及不同操作系统架构(32/64 位)和驱动器(C:\、D:\ 等:)之间的问题。

So far I came across an example given on MSDNyet I can get the script to work in VBS, every run just complains about different errors. Here is what they've got on an example script for .net to get the Sys32folder.

到目前为止,我在MSDN上遇到了一个例子,但我可以让脚本在 VBS 中工作,每次运行只是抱怨不同的错误。这是他们在 .net 的示例脚本中获得的用于获取Sys32文件夹的内容。

' Sample for the Environment.GetFolderPath method
Imports System

Class Sample
   Public Shared Sub Main()
      Console.WriteLine()
      Console.WriteLine("GetFolderPath: {0}", Environment.GetFolderPath(Environment.SpecialFolder.System))
   End Sub 'Main
End Class 'Sample
'
'This example produces the following results:
'
'GetFolderPath: C:\WINNT\System32
'


As Helen mentioned, this is my script to determine the OS Architecture and depending on the outcome I wish to retrieve the respective 'Program Files' path

正如海伦所提到的,这是我确定操作系统架构的脚本,并根据我希望检索相应“程序文件”路径的结果

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\" & sPC  & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
    sSystemArchitecture = objOperatingSystem.OSArchitecture
Next

采纳答案by brettdj

for vbs from How to get program files environment setting from VBScript

for vbs from How to get program files environment setting from VBScript

Set wshShell = CreateObject("WScript.Shell")
WScript.Echo wshShell.ExpandEnvironmentStrings("%PROGRAMFILES%")

if you are in vba

如果你在 vba

Sub GetMe()
Set wshShell = CreateObject("WScript.Shell")
MsgBox wshShell.ExpandEnvironmentStrings("%PROGRAMFILES%")
End Sub