Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting Temporary Windows Dir 2

Status
Not open for further replies.

Rydel

Programmer
Feb 5, 2001
376
CZ
I do it via Windows API function GetTempPath:

Code:
Const MAX_PATH = 255

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

Public Function GetTempDir() As String
    Dim sRet As String, lngLen As Long
    
    'create buffer
    sRet = String(MAX_PATH, 0)

    lngLen = GetTempPath(MAX_PATH, sRet)
    If lngLen = 0 Then err.Raise err.LastDllError
    GetTempDir = Left$(sRet, lngLen)
End Function

It works good enough for me. Though I am wondering if there is some built-in VB function to get TEMP dir?


regards,
rydel n23
 
You can use the GetSpecialFolder method of the File System Object to achieve this.

Set a reference in the project to the Microsoft Scripting Runtime object and use this code
Code:
    Dim FSO As FileSystemObject
    Dim TempDirectory As String

    Set FSO = CreateObject("Scripting.FileSystemObject")
    TempDirectory = FSO.GetSpecialFolder(TemporaryFolder)
 
See an alternative in thread...actually no. The last time I posted this it got deleted, so let's make sure there are at least two copies this time around...

You'll need to add a reference to the Microsoft Shell Controls and Automation Library (if we late-bound then the function wouldn't be able to prompt nicely for the right parameters). And here's the code. It's very short:
[tt]
Public Function GetSpecialPath(vdir As ShellSpecialFolderConstants) As String
Dim myShell As Shell32.Shell

With New Shell32.Shell
' Should work for almost all versions of Shell32.dll
GetSpecialPath = .NameSpace(vdir).ParentFolder.ParseName(.NameSpace(vdir).Title).Path
' If you have shell32.dll version 5.0 or later (XP, W2000, Windows Me) you can use the following instead:
' GetSpecialPath= .NameSpace(vDir).Self.Path
End With

End Function
 
I feel funny about posting this after all the eloquent solutions... but this seems to work (returns the short path name of the temporary folder for the current user):

[tt]MsgBox Environ$("temp")[/tt]

Windows has been providing this information since the first version. Probably for compatability with older programs and programmers.



Real men don't use Interrupt 21h.
 
Oh, I'd just do a quick search in this forum on the keywords "environment" and "variable" over the last 6 months, say, for views and explanations as to why that solution is fallable.
 
Maybe it wasn't worth the star, but I gotta say VB coughed up a message box real quick.
Simplicity is the key in my book, but in the world of programming eloquence is probably more failsafe.
a star to strongm for eloquence

If you invite abuse, it's impolite not to accept it.
Hawkeye Pierce to Frank Burns. MASH c.1980
I try not to invite abuse these days!
 
Code:
MsgBox Environ$("temp")

Maybe it wasn't worth the star, but I gotta say VB coughed up a message box real quick.

Ok, now delete your temp directory and try it again. Any difference?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top