简体   繁体   English

如何在C#中创建随机图像名称?

[英]How do I create a random image name in C#?

当我添加图片时,我希望它创建一个新的随机文件名,因为如果你添加一个具有相同名称的图片,它将只是覆盖。

The is a built-in method Path.GetRandomFileName . 这是一个内置的方法Path.GetRandomFileName It returns a random folder name or file name. 它返回一个随机文件夹名称或文件名。

The GetRandomFileName method returns a cryptographically strong, random string that can be used as either a folder name or a file name. GetRandomFileName方法返回加密强大的随机字符串,可用作文件夹名称或文件名。 Unlike GetTempFileName, GetRandomFileName does not create a file. 与GetTempFileName不同,GetRandomFileName不会创建文件。 When the security of your file system is paramount, this method should be used instead of GetTempFileName. 当文件系统的安全性至关重要时,应该使用此方法而不是GetTempFileName。

If you want to use your extension (eg .jpg instead of generated), you could use another helper method Path.ChangeExtension : 如果你想使用扩展名(例如.jpg而不是生成),你可以使用另一个辅助方法Path.ChangeExtension

string extension = ".jpg";
string fileName = Path.ChangeExtension(
    Path.GetRandomFileName(),
    extension
);

System.IO.Path.GetRandomFileName gets a file name that is guaranteed to be unique . System.IO.Path.GetRandomFileName获取保证唯一的文件名。

As you want to save pictures, you could just use a GUID as the filename: 由于您想保存图片,您可以使用GUID作为文件名:

string filename = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".jpg");

I always do it this way when I need another file extension than .tmp (which files get when you create them via GetTempFileName). 当我需要另一个文件扩展名而不是.tmp(当你通过GetTempFileName创建文件时获取文件扩展名)时,我总是这样做。
Of course you could create the files via GetTempFileName and then rename them, but then you have to check again if a file with the new name exists... 当然你可以通过GetTempFileName创建文件,然后重命名它们,但是你必须再次检查是否存在具有新名称的文件...

You could generate a Guid and use that for your file name. 您可以生成Guid并将其用于您的文件名。 Although this would mean that the files are not human readable and have no information as to what the content is. 虽然这意味着文件不是人类可读的,并且没有关于内容是什么的信息。

You could built it using the current time. 您可以使用当前时间构建它。

string fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".png";

The above example will format the current time using year, month, day, hours, minutes, seconds, and fractions of a second. 上面的示例将使用年,月,日,小时,分钟,秒和分数秒来格式化当前时间。 (The fraction of a second can be specified with fewer f s if you want it down to one.). (如果你希望它减少到1,则可以用较少的f s指定秒的分数。)。


Advantages: 好处:

  • It will sort automatically by the created time in an alphabetically sorted list. 它将按字母顺序排序列表中的创建时间自动排序。 (Like default sorting in Windows Explorer.) (与Windows资源管理器中的默认排序一样。)
  • It is human readable and provides useful information about the time it is created. 它是人类可读的,并提供有关其创建时间的有用信息。

Disadvantages: 缺点:

  • If this is a web application (or other sort of multi-thread process) there is a (small) chance of two files getting same name if generated at the same time. 如果这是一个Web应用程序(或其他类型的多线程进程),如果同时生成两个文件,则有一个(小)机会获得相同的名称。 This is not an issue if this is a single-thread EXE. 如果这是一个单线程EXE,这不是问题。

Name your image using a GUID 使用GUID命名您的图像

For C# you can use: System.Guid.NewGuid().ToString() 对于C#,您可以使用: System.Guid.NewGuid().ToString()

也许您正在寻找GetTempFileName

I would go with awe . 我会敬畏 Use the original filename + DateTime.Now.ToString("yyyyMMddHHmmssffff"); 使用原始文件名+ DateTime.Now.ToString("yyyyMMddHHmmssffff"); .

I would also go with the GUID solution. 我也会选择GUID解决方案。

Here some code I would use regularly: 这里有一些我经常使用的代码:

public static string GetRandomFileName(string extension)
{
    StringBuffer sb = new StringBuffer(Guid.NewGuid().ToString());
    if (!string.IsNullOrEmpty(extensions))
    {
        sb.Append(".");
        sb.Append(extension);
    }

    return sb.ToString();
}

Would provide you with a fine, reusable solution. 将为您提供精细,可重复使用的解决方案。 Put this into your "collection of my greatest moments" - classlibrary and you are good to go. 把它放到你的“我最伟大的时刻的集合” - 类库中,你很高兴。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM