Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: What is the fastest way to generate a unique filename?

I've seen several suggestions on naming files randomly, including using

System.IO.Path.GetRandomFileName()

or using a

System.Guid

and appending a file extension.

My question is: What is the fastest way to generate a unique filename?

like image 769
Dan Esparza Avatar asked Oct 21 '09 18:10

Dan Esparza


6 Answers

A GUID would be extremely fast, since it's implementation guarantees Windows can generate at least 16,384 GUIDS in a 100-nanosecond timespan. (As others pointed out, the spec doesn't guarantee, only allows for. However, GUID generation is really, really fast. Really.) The likelihood of collision on any filesystem anywhere on any network is very low. It's safe enough that although it'd be best practice to always check to see if that filename is available anyway, in reality you would never even need to do that.

So you're looking at no I/O operations except the save itself, and <0.2 milliseconds (on a test machine) to generate the name itself. Pretty fast.

like image 116
Rex M Avatar answered Oct 05 '22 23:10

Rex M


You want System.IO.Path.GetTempFileName()

I can't actually say whether it's fastest or not, but it's the right way to do this, which is more important.

like image 41
Joel Coehoorn Avatar answered Oct 05 '22 23:10

Joel Coehoorn


Well I have been writing file system drivers for 20 years and would say Rex is correct. Generating a guid is much much faster, since it requires far less overhead than searching for a unique file name. GetTempFileName actually creates a file, which means it has to call through the entire file system driver stack (who knows how many calls that would be and a switch into kernel mode.) GetRandomFileName sounds like it is faster, however I would trust the GUID call to be even faster. What people don't realize is that even testing for the existence of a file requires a complete call through the driver stack. It actually results in an open, get attributes and close (which is at least 3 calls, depending on the level.) In reality it is a minimum of 20 function calls and a transition to kernel mode. GUIDS guarentee of uniqueness is good enough for most purposes.

My recommendation is to generate the name and create the file only if it doesn't exist. If it does, throw an exception and catch it, then generate a new guid and try again. That way, you have zero chance of errors and can sleep easy at night.

On a side note, checking for errors is so overdone. Code should be designed to crash if assumptions are wrong, or catch exceptions and deal with it then. Its much faster to push and pop and address on the exception stack, than to check everytime on every function for an error.

like image 25
Jordan Avatar answered Oct 06 '22 00:10

Jordan


If you control the destination where the files will be located, and there is only one process and thread that writes to it, just append some auto-incrementing number to a base name.

If you don't control the destination, or need a multithreaded implementation, use a GUID.

like image 32
LBushkin Avatar answered Oct 06 '22 00:10

LBushkin


Use an Int and increment it for each file.

like image 42
Tom B Avatar answered Oct 05 '22 23:10

Tom B


If you control the directory, you could name your file based on the lastWriteTime:

DirectoryInfo info = new DirectoryInfo(directoryPath);
long uniqueKey = info.LastWriteTime.Ticks+1L;
string filename = String.Format("file{0}.txt", key);

But you have to check performances of this code: I guess building a DirectoryInfo does not come for free.

like image 21
Stéphane Avatar answered Oct 05 '22 22:10

Stéphane