How to create unique file names?

  • Thread starter Thread starter Brett
  • Start date Start date
B

Brett

I need to create unique file names while in a loop. Each interaction will
produce a file name. Also, there may be 10 threads, each with its own loop
producing files, all going at the same time. The files are actually images
being created by a special printer driver.

I thought using a long date would be good. However, I can't find how to
include milliseconds, which is critical. This is the closest I could find:
System.DateTime.Now.Millisecond. Also, I will need to append a random
number to the date, which should ensure uniqueness.

I want to remove all spaces, semicolons, and slashes. This means three
lines of code to perform a Replace() on the same variable. Is there another
way to do this?

Any comments on a completely different approach to producing these unique
names is welcome. Dates aren't necessary. I wasn't sure if a random number
has the probability of coming up again at some point. That's why I chose a
mixture of date and random number.

Thanks,
Brett
 
True. However, from the docs, "Such an identifier has a very low
probability of being duplicated." I'll be creating files constantly. Is
this method actually better than my current method with regards to
uniqueness and performance?

I imagine the GUID is better performance wise.

Thanks,
Brett
 
Brett <[email protected]> said:
Any comments on a completely different approach to producing these unique
names is welcome. Dates aren't necessary. I wasn't sure if a random number
has the probability of coming up again at some point. That's why I chose a
mixture of date and random number.

Guid.NewGuid().ToString("") using whichever format string suits should
give a vanishingly low probability of collisions. You can always use
File.Exists to check before creating it.

If you're really paranoid (and I'm assuming this code isn't flying an
airliner or running a dialysis machine, so probably not) you could
ensure that each thread uses a different filename prefix and then check
File.Exists before creating each file. If only that thread can use that
prefix then there's no possibility of another thread using it between
checking and creating the file. I don't think there is any justification
for doing this, though.

If you don't like the long filenames you'll get from Guids, use a prefix
per thread and a counter maintained in each.
 
Yes. This is better than your current method.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
 
Brett,

The guid fulfils your goal however it looks so horrible.

int Start = Environment.TickCount;
int current = Environment.TickCount-Start;

To fulfil your first question because this are milliseconds.

Just as alternative

Cor
 
Cor Ligthert said:
Brett,

The guid fulfils your goal however it looks so horrible.

int Start = Environment.TickCount;
int current = Environment.TickCount-Start;

To fulfil your first question because this are milliseconds.

Just as alternative

Cor

I'm staying with GUID because it's simpler and has good performance for what
it does.

Thanks,
Brett
 
Brett said:
Any comments on a completely different approach to producing these
unique names is welcome. Dates aren't necessary. I wasn't sure if a
random number has the probability of coming up again at some point.

What about Path.GetTempFileName()?

-mdb
 
Back
Top