Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating temporary folders

Tags:

.net

io

I am working on a program that needs to create a multiple temporary folders for the application. These will not be seen by the user. The app is written in VB.net. I can think of a few ways to do it such as incremental folder name or random numbered folder names, but I was wondering, how other people solve this problem?

like image 767
wusher Avatar asked Aug 19 '08 18:08

wusher


People also ask

How do I create a folder in tmp directory?

To create new directory use "mkdir" command. For example, to create directory TMP in the current directory issue either "mkdir TMP" or "mkdir ./TMP". It's a good practice to organize files by creating directories and putting files inside of them instead of having all files in one directory.

Do temp folders get deleted?

No, there's nothing turned on by default in Windows that will automatically delete temp files. Not on shutdown, not on reboot, not ever. The Disk Cleanup tool has an option to do that when its run.

What is a temp folder in Windows?

The Temp folder is a directory on your Windows PC used to store temporary files. Clearing the Temp folder is a standard procedure for system administration to reduce the amount of storage space used.

Do temporary files do anything?

What are temporary files? Temporary files, also called temp or tmp files, are created by Windows or programs on your computer to hold data while a permanent file is being written or updated. The data will be transferred to a permanent file when the task is complete, or when the program is closed.


2 Answers

Update: Added File.Exists check per comment (2012-Jun-19)

Here's what I've used in VB.NET. Essentially the same as presented, except I usually didn't want to create the folder immediately.

The advantage to use GetRandomFilename is that it doesn't create a file, so you don't have to clean up if your using the name for something other than a file. Like using it for folder name.

Private Function GetTempFolder() As String     Dim folder As String = Path.Combine(Path.GetTempPath, Path.GetRandomFileName)     Do While Directory.Exists(folder) or File.Exists(folder)         folder = Path.Combine(Path.GetTempPath, Path.GetRandomFileName)     Loop      Return folder End Function 

Random Filename Example:

C:\Documents and Settings\username\Local Settings\Temp\u3z5e0co.tvq


Here's a variation using a Guid to get the temp folder name.

Private Function GetTempFolderGuid() As String     Dim folder As String = Path.Combine(Path.GetTempPath, Guid.NewGuid.ToString)     Do While Directory.Exists(folder) or File.Exists(folder)         folder = Path.Combine(Path.GetTempPath, Guid.NewGuid.ToString)     Loop      Return folder End Function 

guid Example:

C:\Documents and Settings\username\Local Settings\Temp\2dbc6db7-2d45-4b75-b27f-0bd492c60496

like image 195
Rick Avatar answered Sep 29 '22 18:09

Rick


You have to use System.IO.Path.GetTempFileName()

Creates a uniquely named, zero-byte temporary file on disk and returns the full path of that file.

You can use System.IO.Path.GetDirectoryName(System.IO.Path.GetTempFileName()) to get only the temp folder information, and create your folders in there

They are created in the windows temp folder and that's consider a best practice

like image 33
juan Avatar answered Sep 29 '22 18:09

juan