Page 1 of 1

Get temp path and temp filename using .Net

Posted: Sat Feb 27, 2010 3:24 pm
by Neo
Use following methods to get Temporary folder and temporary filename.

Code: Select all

System.IO.Path.GetTempPath
System.IO.Path.GetTempFileName
Creating a temp file using a random name

Code: Select all

Private Function GetTempFile() As String
    Dim folder As String = Path.Combine(Path.GetTempPath, Path.GetRandomFileName)
    Do While Directory.Exists(folder)
        folder = Path.Combine(Path.GetTempPath, Path.GetRandomFileName)
    Loop

    Return folder
End Function
ex: C:\Documents and Settings\username\Local Settings\Temp\u3z5e0co.tvq

Creating a temp file using a GUID

Code: Select all

Private Function GetTempFile() As String
    Dim folder As String = Path.Combine(Path.GetTempPath, Guid.NewGuid.ToString)
    Do While Directory.Exists(folder)
        folder = Path.Combine(Path.GetTempPath, Guid.NewGuid.ToString)
    Loop

    Return folder
End Function
ex: C:\Documents and Settings\username\Local Settings\Temp\2dbc6db7-2d45-4b75-b27f-0bd492c60496