获取VBScript中的当前临时目录路径?

时间:2009-01-08 14:04:15

标签: vbscript

获取当前临时目录路径的VB技巧:

Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long 

在VBScript中失败。所以?

5 个答案:

答案 0 :(得分:43)

WScript.CreateObject("Scripting.FileSystemObject").GetSpecialFolder(2)

我花了一段时间才在Google上找到它。所以对于下一个寻找和我一样的人......

答案 1 :(得分:35)

Const WindowsFolder = 0

Const SystemFolder = 1

Const TemporaryFolder = 2

Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")

Dim tempFolder: tempFolder = fso.GetSpecialFolder(TemporaryFolder)

答案 2 :(得分:13)

另一种可能性:

CreateObject("WScript.Shell").ExpandEnvironmentStrings("%Temp%")

答案 3 :(得分:1)

您还可以继续使用GetTempPath API。从vbscript调用API有点棘手。以下是有关如何从vbscript进行Win32 API调用的一些指示:

Reference 1

Reference 2

Reference 3

答案 4 :(得分:0)

完全基于AnthonyWJones' answer,这是我的解决方案:

Public Enum SpecialFolder
    WindowsFolder = 0
    SystemFolder = 1
    TempFolder = 2
End Enum

Public Function GetFolder(folder As Integer) As String
    Dim objFSO  As Object

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    GetFolder = objFSO.GetSpecialFolder(folder)
End Function

例如,您将使用GetFolder(TempFolder)获取用户的临时文件夹的路径名。