tags:

views:

404

answers:

8

What's the best way to get a string containing a folder name that I can be certain does not exist? That is, if I call DirectoryInfo.Exists for the given path, it should return false.

EDIT: The reason behind it is I am writing a test for an error checker, the error checker tests whether the path exists, so I wondered aloud on the best way to get a path that doesn't exist.

A: 

Well, one good bet will be to concatenate strings like the user name, today's date, and time down to the millisecond.

I'm curious though: Why would you want to do this? What should it be for?

Jon Limjap
+5  A: 

Well, without creating the directory, all you can be sure of is that it didn't exist a few microseconds ago. Here's one approach that might be close enough:

        string path = Path.GetTempFileName();
        File.Delete(path);
        Directory.CreateDirectory(path);

Note that this creates the directory to block against thread races (i.e. another process/thread stealing the directory from under you).

Marc Gravell
This gets the path of a temporary folder, but if krolley wants something that would persist, this won't work.
Jon Limjap
True - but hard to know if this is a good or bad thing without more context. Actually, I like jop's Guid suggestion - if I can fix my openid I'll "up" it ;-p
Marc Gravell
AFAIK, in Windows, there is now way to wrap multiple operations in a single atomic transaction. So there really is no absolute answer to this. All we have are 'good enough' answers.
jop
+6  A: 

There isn't really any way to do precisely what you way you want to do. If you think about it, you see that even after the call to DirectoryInfo.Exists has returned false, some other program could have gone ahead and created the directory - this is a race condition.

The normal method to handle this is to create a new temporary directory - if the creation succeeds, then you know it hadn't existed before you created it.

1800 INFORMATION
+9  A: 

Name it after a GUID - just take out the illegal characters.

jop
Does a guid contain any illegal characters? I just had a look and it doesn't seem like it does.
krolley
A: 

Is this to create a temporary folder or something? I often use Guid.NewGuid to get a string to use for the folder name that you want to be sure does not already exist.

jwanagel
A: 

I think you can be close enough:

string directoryName = Guid.NewGuid.ToSrtring();

Since Guid's are fairly random you should not get 2 dirs the same.

mattlant
damn people are fast!
mattlant
makes you wonder if there SO bots answering these quick and easy questions. :)
jop
*matt thinks jop is actually a bot* ;)
mattlant
*Fairly random* is one way of describing a Guid i guess... I suppose it depends on how many lives-of-the-universe you expect your system to be running for!
Aidan
A: 

Using a freshly generated GUID within a namespace that is also somewhat unique (for example, the name of your application/product) should get you what you want. For example, the following code is extremely unlikely to fail:

string ParentPath = System.IO.Path.Combine(Environment.GetEnvironmentVariable("TEMP"), "MyAppName");
string UniquePath = System.IO.Path.Combine(ParentPath, Guid.NewGuid().ToString());
System.IO.Directory.CreateDirectory(UniquePath);
mdb
+4  A: 

What I ended up using:

using System.IO;

string path = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());

(Also, it doesn't seem like you need to strip out chars from a guid - they generate legal filenames)

krolley