【问题标题】:Determining the location of Program Files using VBS使用 VBS 确定程序文件的位置
【发布时间】:2011-11-25 05:28:06
【问题描述】:

使用 VBS 提取“程序文件”目录位置的安全方法是什么?我想从环境中获取目录位置,因为它可以避免本地化问题以及不同操作系统架构(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
'

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

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

【问题讨论】:

  • 你是怎么运行这个的?它看起来更像 VB.NET,而不是 VBS 或 VBA。
  • 在 64 位 Windows 上,有 Program FilesProgram Files (x86)。你需要哪一个?
  • .. 好点海伦。我下面的代码返回 x86
  • @oracle;诚然。抱歉,我错误地将其识别为 vbs。我在vbs中的意思是这样的。 @海伦;你是完全正确的,我首先检查操作系统架构是 32 还是 64,并根据我想找到相应的文件夹。请参阅上面的更新

标签: vba vbscript directory special-folders


【解决方案1】:

来自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

【讨论】:

  • 太好了,谢谢!上面的代码将返回 64 位的“Program Files”文件夹。 x64 环境下的返回 32 位“Program Files”文件夹使用%PROGRAMFILES(x86)%
  • 在VBA中你可以使用Environ("PROGRAMFILES")
猜你喜欢
  • 2013-11-20
  • 2012-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-18
相关资源
最近更新 更多