Will Path.GetRandomFileName
generate a unique filename every single time? Also, what about Path.GetTempFileName
- will that generate a unique name?
The short answer is yes in both cases.
In reality get it will generate 11 random chars that means there are (26 +10)^11 possible names (1.316217e+17) so chances of creating the same name twice are none existand for all practical purposes.
For more information I suggest you read this
and the relevant MSDN pages
Copying from my fiddle at https://dotnetfiddle.net/bmFVSX the Path_SafeGetTempFilename method should give you a bit more safety (but still is prone to low probability data race that could be minimized by using your own temp subfolder):
//.NET's Path.GetTempFilename (https://docs.microsoft.com/en-us/dotnet/api/system.io.path.gettempfilename?view=net-5.0) raises an IOException if the system's temp folder has more than 65535 files
//Following a suggestion from https://github.com/dotnet/sdk/issues/8439
//to use Path.GetTempPath (https://docs.microsoft.com/en-us/dotnet/api/system.io.path.gettemppath?view=net-5.0) to get the system temp folder
//combined via Path.Combine (https://docs.microsoft.com/en-us/dotnet/api/system.io.path.combine?view=net-5.0)
//with GetRandomFilename (https://docs.microsoft.com/en-us/dotnet/api/system.io.path.getrandomfilename?view=net-5.0) instead
//For extra safety cross-checking (in a do/while loop) with File.Exists (https://docs.microsoft.com/en-us/dotnet/api/system.io.file.exists?view=net-5.0) before returning the file
//Using File.WriteAllLines (https://docs.microsoft.com/en-us/dotnet/api/system.io.file.writealllines?view=net-5.0) to create the temp file since that is the original behaviour of GetTempFilename in contrast to GetRandomFilename
//Note: "Path.GetRandomFileName() uses the RNGCryptoServiceProvider which is a cryptographically strong random number generator", quoting https://stackoverflow.com/questions/21684696/will-path-getrandomfilename-generate-a-unique-filename-every-time/21684881#21684881
using System;
using System.Diagnostics;
using System.IO;
public class Program
{
public static string Path_SafeGetTempFileName() {
string filepath; //no need to initialize with empty/null value, the do/while code flow construct will always set a value to filepath
do
{
filepath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
} while (File.Exists(filepath));
try
{
File.WriteAllLines(filepath, new string[]{}); //generate an empty file
}
catch
{
Debug.WriteLine($"Couldn't create empty temporary file: {filepath}");
//TODO: log any failure to generate an empty temp file here
}
//Note: don't do a loop if it fails to create the temp file since it might be failing to access the filesystem for other reasons, which would cause infinite recursion
return filepath;
}
public static void Main()
{
string tempFilePath1 = Path.GetTempFileName();
string tempFilePath2 = Path_SafeGetTempFileName();
Console.WriteLine(tempFilePath1);
Console.WriteLine(tempFilePath2);
}
}
On my system Path.GetRandomFileName()
returns a short name in the 8.3 format.
Is it guaranteed never to return the same name twice? No, you can't guarantee that, just like you can't for any hashing algorithm either. There are only a finite amount of names so eventually you get a duplicate.
However the chance for that is very low since Path.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.