C#中:什么是生成一个唯一的文件名的最快方法? [英] C#: What is the fastest way to generate a unique filename?

查看:148
本文介绍了C#中:什么是生成一个唯一的文件名的最快方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在随机命名的文件,包括使用见过几个建议

I've seen several suggestions on naming files randomly, including using

System.IO.Path.GetRandomFileName()

或使用

System.Guid

和附加文件扩展名。

我的问题是:?什么是生成一个唯一的文件名的最快方法

推荐答案

一个GUID是非常快的,<击>,因为它的实现保证Windows可以产生至少16,384 GUIDS在100纳秒的时间跨度。 (正如其他人所指出的,该规范并不能保证,只允许。但是,GUID生成是真的,真快,真的。)发生碰撞的可能性在任何文件系统上的任何地方任何网络非常低。这是足够安全,虽然它会是最好的做法,经常检查,看看是否该文件名可无论如何,在现实中你永远不会甚至需要做到这一点。

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.

所以,你看没有I / O操作,除了自己救自己,和&lt; 0.2毫秒(在测试机)来生成名称本身。 pretty的快。

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.

这篇关于C#中:什么是生成一个唯一的文件名的最快方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆