Will path.GetRandomFileName generate a unique filename every single time? Also, what about Gettempfilename - will that generate a unique name?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
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
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 theRNGCryptoServiceProvider
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.