gpt4 book ai didi

c# - 已存在文件的概率 System.IO.Path.GetRandomFileName()

转载 作者:太空狗 更新时间:2023-10-30 00:04:49 27 4
gpt4 key购买 nike

最近我得到了异常:

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

这是我用于生成文件名的以下代码的结果:

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

在意识到可以在一个 Tick 中创建两个文件后,我将代码更改为:

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

但我还是想知道在新的情况下出现上述异常的概率是多少?

最佳答案

在内部,GetRandomFileName 使用 RNGCryptoServiceProvider 生成 11 个字符 (name:8+ext:3) 的字符串。该字符串表示 base-32 编码的数字,因此可能的字符串总数为 3211 或 255

假设均匀分布,复制的几率约为 2-55,即 36 千万亿分之一。这太低了:相比之下,您赢得纽约乐透的机会大约是 的一百万倍。

关于c# - 已存在文件的概率 System.IO.Path.GetRandomFileName(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27945559/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com