saadan
saadan

Reputation: 487

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

When I add a picture I want it to create a new random file name because if you add a picture with the same name it will just overwrite.

Upvotes: 8

Views: 6103

Answers (7)

Oleks
Oleks

Reputation: 32343

The is a built-in method 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. Unlike GetTempFileName, GetRandomFileName does not create a file. When the security of your file system is paramount, this method should be used instead of GetTempFileName.

If you want to use your extension (e.g. .jpg instead of generated), you could use another helper method 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.

Upvotes: 7

awe
awe

Reputation: 22462

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 fs if you want it down to one.).


Advantages:

  • It will sort automatically by the created time in an alphabetically sorted list. (Like default sorting in Windows Explorer.)
  • 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. This is not an issue if this is a single-thread EXE.

Upvotes: 3

stormianrootsolver
stormianrootsolver

Reputation: 70

I would also go with the GUID solution.

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.

Upvotes: 0

Christian Specht
Christian Specht

Reputation: 36451

As you want to save pictures, you could just use a GUID as the filename:

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).
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...

Upvotes: 5

hakan
hakan

Reputation: 906

Name your image using a GUID

For C# you can use: System.Guid.NewGuid().ToString()

Upvotes: 3

Matt
Matt

Reputation: 2790

You could generate a Guid and use that for your file name. Although this would mean that the files are not human readable and have no information as to what the content is.

Upvotes: 4

Oded
Oded

Reputation: 499242

Perhaps you are looking for GetTempFileName.

Upvotes: 1

Related Questions