Logo

Programming-Idioms

  • C++
  • C#

Idiom #139 Create temp directory

Create a new temporary folder on filesystem, for writing.

using System.IO;
string newDir = Path.GetTempPath() + Guid.NewGuid();
Directory.CreateDirectory(newDir);
string mkTmpDir()
{
    import std.uuid, std.file, std.path;
    string d = buildPath(tempDir, randomUUID.toString);
    if (!d.exists)
    {
        mkdir(d);
        return d;
    }
    else return null;
}

string s;
while (!s.length)
  s = mkTmpDir;

The standard library doesn't provide a function for that.
mkTmpDir generates a folder in the temp dir that takes a UUID as name. The temp dir is created when a non empty path is returned.

New implementation...
< >
programming-idioms.org