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

时间:2011-09-26 13:26:28

标签: vba vbscript directory special-folders

使用VBS提取“Program Files”目录位置的安全方法是什么?我想从环境中获取目录位置,因为它将避免本地化问题以及不同OS体系结构(32/64位)和驱动器(C:\,D:\等等)之间的问题。

到目前为止,我遇到了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
'

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

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

1 个答案:

答案 0 :(得分:2)

来自How to get program files environment setting from VBScript

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

如果你在vba

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