Get temp path and temp filename using .Net
Posted: Sat Feb 27, 2010 3:24 pm
Use following methods to get Temporary folder and temporary filename.
Creating a temp file using a random name
ex: C:\Documents and Settings\username\Local Settings\Temp\u3z5e0co.tvq
Creating a temp file using a GUID
ex: C:\Documents and Settings\username\Local Settings\Temp\2dbc6db7-2d45-4b75-b27f-0bd492c60496
Code: Select all
System.IO.Path.GetTempPath
System.IO.Path.GetTempFileName
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
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