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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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.