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

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

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

or using a 

and appending a file extension.  
My question is:  What is the fastest way to generate a unique filename?


Answer 1:

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.

Answer 2:

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.

Answer 3:

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.

Answer 4:

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.

Answer 5:

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.

Answer 6:

Use an Int and increment it for each file.

Answer 7:

I hope this self iterative function will help someone to generate a unique filename.

public string getUniqueFileName(int i, string fullpath, string filename)
    string lstDir = fullpath.Substring(0, fullpath.LastIndexOf('\\'));
    string name = Path.GetFileName(fullpath);
    string path = fullpath;

    if (name != filename)
        path = Path.Combine(lstDir, filename);

    if (System.IO.File.Exists(path))
        string ext = Path.GetExtension(name);
        name = Path.GetFileNameWithoutExtension(name);                
        filename = getUniqueFileName(i, fullpath, name + "_" + i + ext);

    return filename;

Answer 8:

You can do something like:

file.MoveTo(deletedfilesdir + @"\" + f.Name + **DateTime.Now.ToFileTimeUtc()** + f.Extension);

Our Awesome Tools