如何在 .NET 中创建 12 个字符长的随机文件夹名称
我想在我的网站上创建随机文件夹名称来存储图像及其缩略图,但我没有使用生成的 guid 的完整版本,而是考虑仅使用其中的一部分,可能只是前 8 个字符,也可能是 base64 编码它。 不过我担心可能发生碰撞。
有人能给我指出正确的方向,看看这是否是一个足够好的主意吗? 如果我想将名称保持在一定数量的字符以下,是否可以使用替代解决方案?
更新:我试图远离 path.GetRandomFileName ,因为它使用原始 guid 并且长度不是 12 个字符......
I would like to create random folder names on my website to store images and their thumbnails, but instead of using the full version of a generated guid, i was thinking about using just part of it, maybe just the first 8 characters and possibly base64 encode it. i am worried about possible collisions though.
Can someone point me in the right direction as to whether or not it is a good enough idea? are there alternative solutions that can be used if i want to keep the name under a certain number of characters?
UPDATE: I am trying to stay away from path.GetRandomFileName , since it uses raw guid and it is not 12 chars long ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为什么不直接使用这样的东西,然后循环直到可以创建没有冲突的文件?
传入 RNG 的原因是为了避免在方法中创建新 RNG(快速连续调用时重复)而不使用静态 RNG(不是线程安全的)的典型问题。
另一种方法是使用单个静态只读 RNG,并通过锁定在
GenerateName
内序列化调用。主要的一点是,不知何故您生成了一个随机名称,并且您只是不断尝试创建具有随机名称的文件,直到成功为止,这可能会很快发生(12 个字符 [AZ 0-9]给出 4,738,381,338,321,616,896 种可能的组合)。
Why not just use something like this, and loop round until you can create the file without conflict?
The reason for passing in the RNG is to avoid the typical problems of creating a new RNG in the method (duplicates when called in quick succession) while not using a static RNG (which isn't threadsafe).
An alternative would be to have a single static readonly RNG, and serialize calls within
GenerateName
by locking.The main point is that somehow you generate a random name, and you just keep trying to create files with random names until you succeed, which is likely to happen very quickly (12 chars [A-Z 0-9] gives 4,738,381,338,321,616,896 possible combinations).
System.IO.Path.GetRandomFileName()
System.IO.Path.GetRandomFileName()
您可以使用 System.IO 中的 Path 类来生成随机文件和文件夹名称等。
You can use the Path class in System.IO to, among other things, generate random file and folder names.
如果您的网站使用数据库,则可以使用自动递增数字或序列来为图像生成唯一的文件名。
使用 Oracle,您将创建一个序列,如下所示;
创建序列 序列名称
最小值
最大值
从价值开始
按值递增
缓存值;
要获取序列中的下一个值,请使用序列的 nextval。 例如
创建序列 seq_user_id;
从双中选择 seq_user_id.NEXTVAL;
INSERT INTO test_bed(用户 ID、类别类型、房间位置)
VALUES (seq_user_id.NEXTVAL, '水下编篮', 'RM1205');
If you're using a database for your web site, then you can use an auto-incrementing number or a sequence to generate unique filenames for the images.
Using Oracle you would create a sequence as follows;
CREATE SEQUENCE sequence_name
MINVALUE value
MAXVALUE value
START WITH value
INCREMENT BY value
CACHE value;
To get the next value in the sequence you use the nextval of the sequence. For example
CREATE SEQUENCE seq_user_id;
SELECT seq_user_id.NEXTVAL FROM dual;
INSERT INTO test_bed (user_id, class_type, room_location)
VALUES (seq_user_id.NEXTVAL, 'Underwater Basketweaving', 'RM1205');