creating files, recursively creating directories

Solution 1:

System.IO.Directory.CreateDirectory() will create all directories and subdirectories in a specified path, should they not already exist.

You can call it, passing the path, to ensure the folder structure is created prior to writing your file.

Solution 2:

here is how I usually do it

Directory.CreateDirectory(Path.GetDirectoryName(filePath));

^ this should take care of ensuring all necessary folders (regardless if some of them already exist) that precedes your file are created. E.g. if you pass it "c:/a/b/c/data/my file.txt", it should ensure "c:/a/b/c/data" path is created.

Solution 3:

While System.IO.Directory.CreateDirectory() will indeed create directories for you recursively, I came across a situation where I had to come up with my own method. Basically, System.IO doesn't support paths over 260 characters, which forced me to use the Delimon.Win32.IO library, which works with long paths, but doesn't create directories recursively.

Here's the code I used for creating directories recursively:

void CreateDirectoryRecursively(string path)
{
    string[] pathParts = path.Split('\\');

    for (int i = 0; i < pathParts.Length; i++)
    {
        if (i > 0)
            pathParts[i] = Path.Combine(pathParts[i - 1], pathParts[i]);

        if (!Directory.Exists(pathParts[i]))
            Directory.CreateDirectory(pathParts[i]);
    }
}

Solution 4:

So, the above didn't work super well for me for basic directory creation. I modified this a bit to handle common cases for drive letters and a path with a file resource on the end.

    public bool CreateDirectoryRecursively(string path)
    {
        try
        {
            string[] pathParts = path.Split('\\');
            for (var i = 0; i < pathParts.Length; i++)
            {
                // Correct part for drive letters
                if (i == 0 && pathParts[i].Contains(":"))
                {
                    pathParts[i] = pathParts[i] + "\\";
                } // Do not try to create last part if it has a period (is probably the file name)
                else if (i == pathParts.Length-1 && pathParts[i].Contains("."))
                {
                    return true;
                }
                if (i > 0) { 
                    pathParts[i] = Path.Combine(pathParts[i - 1], pathParts[i]);
                }
                if (!Directory.Exists(pathParts[i]))
                {
                    Directory.CreateDirectory(pathParts[i]);
                }
            }
            return true;
        }
        catch (Exception ex)
        {
            var recipients = _emailErrorDefaultRecipients;
            var subject = "ERROR: Failed To Create Directories in " + this.ToString() + " path: " + path;
            var errorMessage = Error.BuildErrorMessage(ex, subject);
            Email.SendMail(recipients, subject, errorMessage);
            Console.WriteLine(errorMessage);
            return false;

        }

    }