FileData.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Runtime.InteropServices;
  5. namespace MediaBrowser.Controller.IO
  6. {
  7. /// <summary>
  8. /// Provides low level File access that is much faster than the File/Directory api's
  9. /// </summary>
  10. public static class FileData
  11. {
  12. public const int MAX_PATH = 260;
  13. public const int MAX_ALTERNATE = 14;
  14. /// <summary>
  15. /// Gets information about a path
  16. /// </summary>
  17. public static WIN32_FIND_DATA GetFileData(string path)
  18. {
  19. WIN32_FIND_DATA data;
  20. IntPtr handle = FindFirstFile(path, out data);
  21. if (handle == IntPtr.Zero)
  22. throw new IOException("FindFirstFile failed");
  23. FindClose(handle);
  24. data.Path = path;
  25. return data;
  26. }
  27. /// <summary>
  28. /// Gets all file system entries within a foler
  29. /// </summary>
  30. public static IEnumerable<WIN32_FIND_DATA> GetFileSystemEntries(string path, string searchPattern)
  31. {
  32. return GetFileSystemEntries(path, searchPattern, true, true);
  33. }
  34. /// <summary>
  35. /// Gets all files within a folder
  36. /// </summary>
  37. public static IEnumerable<WIN32_FIND_DATA> GetFiles(string path, string searchPattern)
  38. {
  39. return GetFileSystemEntries(path, searchPattern, true, false);
  40. }
  41. /// <summary>
  42. /// Gets all sub-directories within a folder
  43. /// </summary>
  44. public static IEnumerable<WIN32_FIND_DATA> GetDirectories(string path, string searchPattern)
  45. {
  46. return GetFileSystemEntries(path, searchPattern, false, true);
  47. }
  48. /// <summary>
  49. /// Gets all file system entries within a foler
  50. /// </summary>
  51. public static IEnumerable<WIN32_FIND_DATA> GetFileSystemEntries(string path, string searchPattern, bool includeFiles, bool includeDirectories)
  52. {
  53. string lpFileName = Path.Combine(path, searchPattern);
  54. WIN32_FIND_DATA lpFindFileData;
  55. var handle = FindFirstFile(lpFileName, out lpFindFileData);
  56. if (handle == IntPtr.Zero)
  57. {
  58. int hr = Marshal.GetLastWin32Error();
  59. if (hr != 2 && hr != 0x12)
  60. {
  61. throw new IOException("GetFileSystemEntries failed");
  62. }
  63. yield break;
  64. }
  65. if (IncludeInOutput(lpFindFileData.cFileName, lpFindFileData.dwFileAttributes, includeFiles, includeDirectories))
  66. {
  67. yield return lpFindFileData;
  68. }
  69. while (FindNextFile(handle, out lpFindFileData) != IntPtr.Zero)
  70. {
  71. if (IncludeInOutput(lpFindFileData.cFileName, lpFindFileData.dwFileAttributes, includeFiles, includeDirectories))
  72. {
  73. lpFindFileData.Path = Path.Combine(path, lpFindFileData.cFileName);
  74. yield return lpFindFileData;
  75. }
  76. }
  77. FindClose(handle);
  78. }
  79. private static bool IncludeInOutput(string cFileName, FileAttributes attributes, bool includeFiles, bool includeDirectories)
  80. {
  81. if (cFileName.Equals(".", StringComparison.OrdinalIgnoreCase))
  82. {
  83. return false;
  84. }
  85. if (cFileName.Equals("..", StringComparison.OrdinalIgnoreCase))
  86. {
  87. return false;
  88. }
  89. if (!includeFiles && !attributes.HasFlag(FileAttributes.Directory))
  90. {
  91. return false;
  92. }
  93. if (!includeDirectories && attributes.HasFlag(FileAttributes.Directory))
  94. {
  95. return false;
  96. }
  97. return true;
  98. }
  99. [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  100. private static extern IntPtr FindFirstFile(string fileName, out WIN32_FIND_DATA data);
  101. [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  102. private static extern IntPtr FindNextFile(IntPtr hFindFile, out WIN32_FIND_DATA data);
  103. [DllImport("kernel32")]
  104. private static extern bool FindClose(IntPtr hFindFile);
  105. }
  106. [StructLayout(LayoutKind.Sequential)]
  107. public struct FILETIME
  108. {
  109. public uint dwLowDateTime;
  110. public uint dwHighDateTime;
  111. }
  112. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  113. public struct WIN32_FIND_DATA
  114. {
  115. public FileAttributes dwFileAttributes;
  116. public FILETIME ftCreationTime;
  117. public FILETIME ftLastAccessTime;
  118. public FILETIME ftLastWriteTime;
  119. public int nFileSizeHigh;
  120. public int nFileSizeLow;
  121. public int dwReserved0;
  122. public int dwReserved1;
  123. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = FileData.MAX_PATH)]
  124. public string cFileName;
  125. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = FileData.MAX_ALTERNATE)]
  126. public string cAlternate;
  127. public bool IsHidden
  128. {
  129. get
  130. {
  131. return dwFileAttributes.HasFlag(FileAttributes.Hidden);
  132. }
  133. }
  134. public bool IsSystemFile
  135. {
  136. get
  137. {
  138. return dwFileAttributes.HasFlag(FileAttributes.System);
  139. }
  140. }
  141. public bool IsDirectory
  142. {
  143. get
  144. {
  145. return dwFileAttributes.HasFlag(FileAttributes.Directory);
  146. }
  147. }
  148. public DateTime CreationTime
  149. {
  150. get
  151. {
  152. return ParseFileTime(ftCreationTime);
  153. }
  154. }
  155. public DateTime LastAccessTime
  156. {
  157. get
  158. {
  159. return ParseFileTime(ftLastAccessTime);
  160. }
  161. }
  162. public DateTime LastWriteTime
  163. {
  164. get
  165. {
  166. return ParseFileTime(ftLastWriteTime);
  167. }
  168. }
  169. private DateTime ParseFileTime(FILETIME filetime)
  170. {
  171. long highBits = filetime.dwHighDateTime;
  172. highBits = highBits << 32;
  173. return DateTime.FromFileTime(highBits + (long)filetime.dwLowDateTime);
  174. }
  175. public string Path { get; set; }
  176. }
  177. }