FileSystem.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. namespace Utilities
  6. {
  7. public abstract class FileSystem : IFileSystem
  8. {
  9. public abstract FileSystemEntry GetEntry(string path);
  10. public abstract FileSystemEntry CreateFile(string path);
  11. public abstract FileSystemEntry CreateDirectory(string path);
  12. public abstract void Move(string source, string destination);
  13. public abstract void Delete(string path);
  14. public abstract List<FileSystemEntry> ListEntriesInDirectory(string path);
  15. public abstract Stream OpenFile(string path, FileMode mode, FileAccess access, FileShare share);
  16. public abstract void SetAttributes(string path, bool? isHidden, bool? isReadonly, bool? isArchived);
  17. public abstract void SetDates(string path, DateTime? creationDT, DateTime? lastWriteDT, DateTime? lastAccessDT);
  18. public List<FileSystemEntry> ListEntriesInRootDirectory()
  19. {
  20. return ListEntriesInDirectory(@"\");
  21. }
  22. public void CopyFile(string sourcePath, string destinationPath)
  23. {
  24. const int bufferLength = 1024 * 1024;
  25. FileSystemEntry sourceFile = GetEntry(sourcePath);
  26. FileSystemEntry destinationFile = GetEntry(destinationPath);
  27. if (sourceFile == null | sourceFile.IsDirectory)
  28. {
  29. throw new FileNotFoundException();
  30. }
  31. if (destinationFile != null && destinationFile.IsDirectory)
  32. {
  33. throw new ArgumentException("Destination cannot be a directory");
  34. }
  35. if (destinationFile == null)
  36. {
  37. destinationFile = CreateFile(destinationPath);
  38. }
  39. Stream sourceStream = OpenFile(sourcePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  40. Stream destinationStream = OpenFile(destinationPath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
  41. while (sourceStream.Position < sourceStream.Length)
  42. {
  43. int readSize = (int)Math.Max(bufferLength, sourceStream.Length - sourceStream.Position);
  44. byte[] buffer = new byte[readSize];
  45. sourceStream.Read(buffer, 0, buffer.Length);
  46. destinationStream.Write(buffer, 0, buffer.Length);
  47. }
  48. sourceStream.Close();
  49. destinationStream.Close();
  50. }
  51. public abstract string Name
  52. {
  53. get;
  54. }
  55. public abstract long Size
  56. {
  57. get;
  58. }
  59. public abstract long FreeSpace
  60. {
  61. get;
  62. }
  63. public static string GetParentDirectory(string path)
  64. {
  65. if (path == String.Empty)
  66. {
  67. path = @"\";
  68. }
  69. if (!path.StartsWith(@"\"))
  70. {
  71. throw new ArgumentException("Invalid path");
  72. }
  73. if (path.Length > 1 && path.EndsWith(@"\"))
  74. {
  75. path = path.Substring(0, path.Length - 1);
  76. }
  77. int separatorIndex = path.LastIndexOf(@"\");
  78. return path.Substring(0, separatorIndex + 1);
  79. }
  80. /// <summary>
  81. /// Will append a trailing slash to a directory path if not already present
  82. /// </summary>
  83. /// <param name="path"></param>
  84. /// <returns></returns>
  85. public static string GetDirectoryPath(string path)
  86. {
  87. if (path.EndsWith(@"\"))
  88. {
  89. return path;
  90. }
  91. else
  92. {
  93. return path + @"\";
  94. }
  95. }
  96. }
  97. }