FileSystem.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using MediaBrowser.Model.Logging;
  2. using System;
  3. using System.IO;
  4. namespace MediaBrowser.Controller.IO
  5. {
  6. /// <summary>
  7. /// Class FileSystem
  8. /// </summary>
  9. public static class FileSystem
  10. {
  11. /// <summary>
  12. /// Gets the creation time UTC.
  13. /// </summary>
  14. /// <param name="info">The info.</param>
  15. /// <param name="logger">The logger.</param>
  16. /// <returns>DateTime.</returns>
  17. public static DateTime GetLastWriteTimeUtc(FileSystemInfo info, ILogger logger)
  18. {
  19. // This could throw an error on some file systems that have dates out of range
  20. try
  21. {
  22. return info.LastWriteTimeUtc;
  23. }
  24. catch (Exception ex)
  25. {
  26. logger.ErrorException("Error determining LastAccessTimeUtc for {0}", ex, info.FullName);
  27. return DateTime.MinValue;
  28. }
  29. }
  30. /// <summary>
  31. /// Copies all.
  32. /// </summary>
  33. /// <param name="source">The source.</param>
  34. /// <param name="target">The target.</param>
  35. /// <exception cref="System.ArgumentNullException">source</exception>
  36. /// <exception cref="System.ArgumentException">The source and target directories are the same</exception>
  37. public static void CopyAll(string source, string target)
  38. {
  39. if (string.IsNullOrEmpty(source))
  40. {
  41. throw new ArgumentNullException("source");
  42. }
  43. if (string.IsNullOrEmpty(target))
  44. {
  45. throw new ArgumentNullException("target");
  46. }
  47. if (source.Equals(target, StringComparison.OrdinalIgnoreCase))
  48. {
  49. throw new ArgumentException("The source and target directories are the same");
  50. }
  51. // Check if the target directory exists, if not, create it.
  52. Directory.CreateDirectory(target);
  53. foreach (var file in Directory.EnumerateFiles(source))
  54. {
  55. File.Copy(file, Path.Combine(target, Path.GetFileName(file)), true);
  56. }
  57. // Copy each subdirectory using recursion.
  58. foreach (var dir in Directory.EnumerateDirectories(source))
  59. {
  60. CopyAll(dir, Path.Combine(target, Path.GetFileName(dir)));
  61. }
  62. }
  63. }
  64. }