Willcode4cash icon

Create date-based folder structure (.NET Core)

Willcode4cash | PRO | 06/16/17 05:48:00 AM UTC | 0 ⭐ | 272 👁️ | Never ⏰ | []
C# |

980 B

|

None

|

0 👍

/

0 👎

public static DirectoryInfo CreateDateBasedFolderStructure(this DateTime now, DirectoryInfo rootFolder)
{
    try {
        var result = rootFolder
            .CreateSubdirectory(now.Year.ToString())
            .CreateSubdirectory(now.Month.ToString().PadLeft(2, '0'))
            .CreateSubdirectory(now.Day.ToString().PadLeft(2, '0'));
        return result;
    } 
    catch(ArgumentNullException ex)
    {
        // Null path
        throw ex;
    } 
    catch(ArgumentException ex) 
    {
        // Invalid path
        throw ex;
    }
    catch(DirectoryNotFoundException ex)
    {
        // Missing path
        throw ex;
    }
    catch(PathTooLongException ex)
    {
        // Folder or path name is too long
        throw ex;
    }
    catch(IOException ex)
    {
        // Directory or file cannot be created
        throw ex;
    }
    catch(Exception ex) 
    {
        // Any other exception that wasn't already handled
        throw ex;
    }
}

Comments