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

ID : 10227

viewed : 15

Tags : c#.netfiletemporary-filesc#

Top 5 Answer for c# - How can I create a temp file with a specific extension with .NET?

vote vote


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.

vote vote


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.

vote vote


public static string GetTempFileName(string extension) {   int attempt = 0;   while (true)   {     string fileName = Path.GetRandomFileName();     fileName = Path.ChangeExtension(fileName, extension);     fileName = Path.Combine(Path.GetTempPath(), fileName);      try     {       using (new FileStream(fileName, FileMode.CreateNew)) { }       return fileName;     }     catch (IOException ex)     {       if (++attempt == 10)         throw new IOException("No unique temporary file name is available.", ex);     }   } } 

Note: this works like Path.GetTempFileName. An empty file is created to reserve the file name. It makes 10 attempts, in case of collisions generated by Path.GetRandomFileName();

vote vote


You can also alternatively use System.CodeDom.Compiler.TempFileCollection.

string tempDirectory = @"c:\\temp"; TempFileCollection coll = new TempFileCollection(tempDirectory, true); string filename = coll.AddExtension("txt", true); File.WriteAllText(Path.Combine(tempDirectory,filename),"Hello World"); 

Here I used a txt extension but you can specify whatever you want. I also set the keep flag to true so that the temp file is kept around after use. Unfortunately, TempFileCollection creates one random file per extension. If you need more temp files, you can create multiple instances of TempFileCollection.

vote vote


Why not checking if the file exists?

string fileName; do {     fileName = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".csv"; } while (System.IO.File.Exists(fileName)); 

Top 3 video Explaining c# - How can I create a temp file with a specific extension with .NET?