Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a temp file with a specific extension with .NET?

I need to generate a unique temporary file with a .csv extension.

What I do right now is

string filepath = System.IO.Path.GetTempFileName().Replace(".tmp", ".csv"); 

However, this doesn't guarantee that my .csv file will be unique.

I know the chances I ever got a collision are very low (especially if you consider that I don't delete the .tmp files), but this code doesn't looks good to me.

Of course I could manually generate random file names until I eventually find a unique one (which shouldn't be a problem), but I'm curious to know if others have found a nice way to deal with this problem.

like image 239
Brann Avatar asked Feb 24 '09 12:02

Brann


People also ask

What is the file extension for temporary file?

Temporary files typically have a . TMP or . TEMP file extension, but any naming convention might be used. An application is responsible for deleting its temporary files; however, such files often remain on disk if the computer crashed, and the program was not closed properly.

How do I create a temp file?

To create and use a temporary fileThe application opens the user-provided source text file by using CreateFile. The application retrieves a temporary file path and file name by using the GetTempPath and GetTempFileName functions, and then uses CreateFile to create the temporary file.

How do I create a temporary file in CPP?

C++ tmpfile()The tmpfile() function in C++ creates and opens a temporary file in binary read/write (wb+) mode with a unique auto-generated filename. The file will be automatically deleted when it is closed by the program(by executing fclose) or when the program terminates.

How do I create a temp file in PowerShell?

To create a temporary file with the PowerShell, we can use the New-TemporaryFile command. This command creates a temporary file tmp<NNNN>. tmp where NNNN represents the random hexadecimal number.


2 Answers

Guaranteed to be (statistically) unique:

string fileName = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".csv";  

(To quote from the wiki article on the probabilty of a collision:

...one's annual risk of being hit by a meteorite is estimated to be one chance in 17 billion [19], that means the probability is about 0.00000000006 (6 × 10−11), equivalent to the odds of creating a few tens of trillions of UUIDs in a year and having one duplicate. In other words, only after generating 1 billion UUIDs every second for the next 100 years, the probability of creating just one duplicate would be about 50%. The probability of one duplicate would be about 50% if every person on earth owns 600 million UUIDs

EDIT: Please also see JaredPar's comments.

like image 156
Mitch Wheat Avatar answered Sep 28 '22 19:09

Mitch Wheat


Try this function ...

public static string GetTempFilePathWithExtension(string extension) {   var path = Path.GetTempPath();   var fileName = Guid.NewGuid().ToString() + extension;   return Path.Combine(path, fileName); } 

It will return a full path with the extension of your choice.

Note, it's not guaranteed to produce a unique file name since someone else could have technically already created that file. However the chances of someone guessing the next guid produced by your app and creating it is very very low. It's pretty safe to assume this will be unique.

like image 24
JaredPar Avatar answered Sep 28 '22 18:09

JaredPar