how to generate a unique working folder

  • Thread starter Thread starter Mullin Yu
  • Start date Start date
M

Mullin Yu

hi,

i have a web service that has file operations on Windows OS, and there may
be a file concurrency issue if only one working directory e.g. c:\working

therefore, i want to have a unique sub directory for each request so as to
get rid of any file concurrency issue, e.g

c:\working\unique-identifier1 for Request #1
c:\working\unique-identifier1 for Request #2

of course, house-keep is then required.

any suggestions to do so?

thanks!
 
Hi,

Consider using a GUID for each working sub-directory:

eg.
System.Guid guid = System.Guid.NewGuid();
string strSubFolder = @"c:\working\" + guid.ToString();

Then you can use a collection (CollectionBase or your own CollectionBase
inherited class) with key = Request No. / ID and value = strSubFolder for
house-keeping.

HTH
 
1. any size limitation of GUID? how many GUID could be generated?
2. will it be overflow? or will it reuse the old GUID if all the values are
used?

thanks!
 
1. any size limitation of GUID? how many GUID could > be generated?
2. will it be overflow? or will it reuse the old GUID if all the values
are used?

I'll quote directly from the .NET documentation:

"A GUID is a 128-bit integer (16 bytes) that can be used across all
computers and networks wherever a unique identifier is required. Such an
identifier has a very low probability of being duplicated."

The maximum number of possible unique GUID's is 2^128 or
3.4028236692093846346337460743177e+38. A new unique GUID is generated every
time you call the System.Guid.NewGuid() method.

HTH
 
You can always check to see if the directory exist, if not then use
the new guid otherwise generate another one.
 
qwerty said:
You can always check if the directory exist, if exists then generate
another guid.

Probably not worth it in final analysis. AFAIK GUID's are pretty much
guaranteed to be unique in time and space. Considering that GUIDs would be
sufficient to have a million for each cubic lightyear in the entire
universe, you can see that the solution space is quite large (to say the
least). Then, the GUID generation algorithm is designed to avoid collisions.
In fact, IIRC, you could only get a GUID collision on current hardware
before the heat death of the universe if the two machines shared the same
MAC address. As for the on the same machine, I'm pretty sure you'll watch
the Gnab Big before you got a collision.

Putting in checking code for such an infinitesimally small chance is
probably just a waste of processor cycles. If it makes you happier to put
the check in though, go ahead, since I doubt the performance impact will
have any realistic impact on your code.

As you can see, I'm a bit conflicted as to how I'd approach this ;D
 
Back
Top