Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Probability of already existing file System.IO.Path.GetRandomFileName()

Recently I got the exception:

Message:
System.IO.IOException: The file 'C:\Windows\TEMP\635568456627146499.xlsx' already exists.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)

This was the result of the following code I used for generating file names:

Path.Combine(Path.GetTempPath(), DateTime.Now.Ticks + ".xlsx");

After realising that it is possible to create two files in one Tick, I changed the code to:

Path.Combine(Path.GetTempPath(), Path.GetRandomFileName() + ".xlsx");

But I am still wondering what is the probability of the above exception in the new case?

like image 753
gyosifov Avatar asked Jan 14 '15 14:01

gyosifov


People also ask

Is Path GetRandomFileName () unique?

GetRandomFileName() uses the RNGCryptoServiceProvider which is a cryptographically strong random number generator. To summarize it, you can't guarantee in a strict way that it will be unique. But the chance for a duplicate is very low so you can assume it is.

How would you make a unique filename by adding a number?

This is just a string operation; find the location in the filename string where you want to insert the number, and re-construct a new string with the number inserted. To make it re-usable, you might want to look for a number in that location, and parse it out into an integer, so you can increment it.


3 Answers

Internally, GetRandomFileName uses RNGCryptoServiceProvider to generate 11-character (name:8+ext:3) string. The string represents a base-32 encoded number, so the total number of possible strings is 3211 or 255.

Assuming uniform distribution, the chances of making a duplicate are about 2-55, or 1 in 36 quadrillion. That's pretty low: for comparison, your chances of winning NY lotto are roughly one million times higher.

like image 57
Sergey Kalinichenko Avatar answered Sep 30 '22 13:09

Sergey Kalinichenko


The probability of getting duplicate names with GetRandomFileName are really low, but if you look at it source here, you see that they don't check if the name is duplicate (They can't because you can't tell the path where this file should be created)

Instead the Path.GetTempFileName return an unique file name inside the Temp directory.
(So removing also the need to build the temp path in your code)
GetTempFileName uses the Win32 API GetTempFileName requesting the creation of an unique file name.
The Win32 API creates the file with a zero length and release the handle. So you don't fall in concurrency scenarios. Better use this one.

like image 26
Steve Avatar answered Sep 30 '22 13:09

Steve


GetRandomFileName() returns 8.3 char string. This is 11 characters that can vary. Assuming it contains only letters and digits, this gives us an "alphabet" of 36 characters. So the number of variations is least 36^11, which makes the probability of above exception extremely low.

like image 40
andreycha Avatar answered Sep 30 '22 15:09

andreycha