Advertisement
Willcode4cash

Create date-based folder structure (.NET Core)

Jun 16th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.98 KB | None | 0 0
  1. public static DirectoryInfo CreateDateBasedFolderStructure(this DateTime now, DirectoryInfo rootFolder)
  2. {
  3.     try {
  4.         var result = rootFolder
  5.             .CreateSubdirectory(now.Year.ToString())
  6.             .CreateSubdirectory(now.Month.ToString().PadLeft(2, '0'))
  7.             .CreateSubdirectory(now.Day.ToString().PadLeft(2, '0'));
  8.         return result;
  9.     }
  10.     catch(ArgumentNullException ex)
  11.     {
  12.         // Null path
  13.         throw ex;
  14.     }
  15.     catch(ArgumentException ex)
  16.     {
  17.         // Invalid path
  18.         throw ex;
  19.     }
  20.     catch(DirectoryNotFoundException ex)
  21.     {
  22.         // Missing path
  23.         throw ex;
  24.     }
  25.     catch(PathTooLongException ex)
  26.     {
  27.         // Folder or path name is too long
  28.         throw ex;
  29.     }
  30.     catch(IOException ex)
  31.     {
  32.         // Directory or file cannot be created
  33.         throw ex;
  34.     }
  35.     catch(Exception ex)
  36.     {
  37.         // Any other exception that wasn't already handled
  38.         throw ex;
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement