IFileSystem.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. namespace MediaBrowser.Model.IO
  6. {
  7. /// <summary>
  8. /// Interface IFileSystem
  9. /// </summary>
  10. public interface IFileSystem
  11. {
  12. void AddShortcutHandler(IShortcutHandler handler);
  13. /// <summary>
  14. /// Determines whether the specified filename is shortcut.
  15. /// </summary>
  16. /// <param name="filename">The filename.</param>
  17. /// <returns><c>true</c> if the specified filename is shortcut; otherwise, <c>false</c>.</returns>
  18. bool IsShortcut(string filename);
  19. /// <summary>
  20. /// Resolves the shortcut.
  21. /// </summary>
  22. /// <param name="filename">The filename.</param>
  23. /// <returns>System.String.</returns>
  24. string ResolveShortcut(string filename);
  25. /// <summary>
  26. /// Creates the shortcut.
  27. /// </summary>
  28. /// <param name="shortcutPath">The shortcut path.</param>
  29. /// <param name="target">The target.</param>
  30. void CreateShortcut(string shortcutPath, string target);
  31. string MakeAbsolutePath(string folderPath, string filePath);
  32. /// <summary>
  33. /// Returns a <see cref="FileSystemMetadata" /> object for the specified file or directory path.
  34. /// </summary>
  35. /// <param name="path">A path to a file or directory.</param>
  36. /// <returns>A <see cref="FileSystemMetadata" /> object.</returns>
  37. /// <remarks>If the specified path points to a directory, the returned <see cref="FileSystemMetadata" /> object's
  38. /// <see cref="FileSystemMetadata.IsDirectory" /> property will be set to true and all other properties will reflect the properties of the directory.</remarks>
  39. FileSystemMetadata GetFileSystemInfo(string path);
  40. /// <summary>
  41. /// Returns a <see cref="FileSystemMetadata" /> object for the specified file path.
  42. /// </summary>
  43. /// <param name="path">A path to a file.</param>
  44. /// <returns>A <see cref="FileSystemMetadata" /> object.</returns>
  45. /// <remarks><para>If the specified path points to a directory, the returned <see cref="FileSystemMetadata" /> object's
  46. /// <see cref="FileSystemMetadata.IsDirectory" /> property and the <see cref="FileSystemMetadata.Exists" /> property will both be set to false.</para>
  47. /// <para>For automatic handling of files <b>and</b> directories, use <see cref="M:IFileSystem.GetFileSystemInfo(System.String)" />.</para></remarks>
  48. FileSystemMetadata GetFileInfo(string path);
  49. /// <summary>
  50. /// Returns a <see cref="FileSystemMetadata" /> object for the specified directory path.
  51. /// </summary>
  52. /// <param name="path">A path to a directory.</param>
  53. /// <returns>A <see cref="FileSystemMetadata" /> object.</returns>
  54. /// <remarks><para>If the specified path points to a file, the returned <see cref="FileSystemMetadata" /> object's
  55. /// <see cref="FileSystemMetadata.IsDirectory" /> property will be set to true and the <see cref="FileSystemMetadata.Exists" /> property will be set to false.</para>
  56. /// <para>For automatic handling of files <b>and</b> directories, use <see cref="M:IFileSystem.GetFileSystemInfo(System.String)" />.</para></remarks>
  57. FileSystemMetadata GetDirectoryInfo(string path);
  58. /// <summary>
  59. /// Gets the valid filename.
  60. /// </summary>
  61. /// <param name="filename">The filename.</param>
  62. /// <returns>System.String.</returns>
  63. string GetValidFilename(string filename);
  64. /// <summary>
  65. /// Gets the creation time UTC.
  66. /// </summary>
  67. /// <param name="info">The information.</param>
  68. /// <returns>DateTime.</returns>
  69. DateTime GetCreationTimeUtc(FileSystemMetadata info);
  70. /// <summary>
  71. /// Gets the creation time UTC.
  72. /// </summary>
  73. /// <param name="path">The path.</param>
  74. /// <returns>DateTime.</returns>
  75. DateTime GetCreationTimeUtc(string path);
  76. /// <summary>
  77. /// Gets the last write time UTC.
  78. /// </summary>
  79. /// <param name="info">The information.</param>
  80. /// <returns>DateTime.</returns>
  81. DateTime GetLastWriteTimeUtc(FileSystemMetadata info);
  82. /// <summary>
  83. /// Gets the last write time UTC.
  84. /// </summary>
  85. /// <param name="path">The path.</param>
  86. /// <returns>DateTime.</returns>
  87. DateTime GetLastWriteTimeUtc(string path);
  88. /// <summary>
  89. /// Gets the file stream.
  90. /// </summary>
  91. /// <param name="path">The path.</param>
  92. /// <param name="mode">The mode.</param>
  93. /// <param name="access">The access.</param>
  94. /// <param name="share">The share.</param>
  95. /// <param name="isAsync">if set to <c>true</c> [is asynchronous].</param>
  96. /// <returns>FileStream.</returns>
  97. Stream GetFileStream(string path, FileOpenMode mode, FileAccessMode access, FileShareMode share, bool isAsync = false);
  98. Stream GetFileStream(string path, FileOpenMode mode, FileAccessMode access, FileShareMode share,
  99. FileOpenOptions fileOpenOptions);
  100. string DefaultDirectory { get; }
  101. /// <summary>
  102. /// Swaps the files.
  103. /// </summary>
  104. /// <param name="file1">The file1.</param>
  105. /// <param name="file2">The file2.</param>
  106. void SwapFiles(string file1, string file2);
  107. bool AreEqual(string path1, string path2);
  108. /// <summary>
  109. /// Determines whether [contains sub path] [the specified parent path].
  110. /// </summary>
  111. /// <param name="parentPath">The parent path.</param>
  112. /// <param name="path">The path.</param>
  113. /// <returns><c>true</c> if [contains sub path] [the specified parent path]; otherwise, <c>false</c>.</returns>
  114. bool ContainsSubPath(string parentPath, string path);
  115. /// <summary>
  116. /// Determines whether [is root path] [the specified path].
  117. /// </summary>
  118. /// <param name="path">The path.</param>
  119. /// <returns><c>true</c> if [is root path] [the specified path]; otherwise, <c>false</c>.</returns>
  120. bool IsRootPath(string path);
  121. /// <summary>
  122. /// Normalizes the path.
  123. /// </summary>
  124. /// <param name="path">The path.</param>
  125. /// <returns>System.String.</returns>
  126. string NormalizePath(string path);
  127. /// <summary>
  128. /// Gets the file name without extension.
  129. /// </summary>
  130. /// <param name="info">The information.</param>
  131. /// <returns>System.String.</returns>
  132. string GetFileNameWithoutExtension(FileSystemMetadata info);
  133. /// <summary>
  134. /// Determines whether [is path file] [the specified path].
  135. /// </summary>
  136. /// <param name="path">The path.</param>
  137. /// <returns><c>true</c> if [is path file] [the specified path]; otherwise, <c>false</c>.</returns>
  138. bool IsPathFile(string path);
  139. /// <summary>
  140. /// Deletes the file.
  141. /// </summary>
  142. /// <param name="path">The path.</param>
  143. void DeleteFile(string path);
  144. /// <summary>
  145. /// Gets the directories.
  146. /// </summary>
  147. /// <param name="path">The path.</param>
  148. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  149. /// <returns>IEnumerable&lt;DirectoryInfo&gt;.</returns>
  150. IEnumerable<FileSystemMetadata> GetDirectories(string path, bool recursive = false);
  151. /// <summary>
  152. /// Gets the files.
  153. /// </summary>
  154. IEnumerable<FileSystemMetadata> GetFiles(string path, bool recursive = false);
  155. IEnumerable<FileSystemMetadata> GetFiles(string path, string[] extensions, bool enableCaseSensitiveExtensions, bool recursive);
  156. /// <summary>
  157. /// Gets the file system entries.
  158. /// </summary>
  159. /// <param name="path">The path.</param>
  160. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  161. /// <returns>IEnumerable&lt;FileSystemMetadata&gt;.</returns>
  162. IEnumerable<FileSystemMetadata> GetFileSystemEntries(string path, bool recursive = false);
  163. /// <summary>
  164. /// Gets the directory paths.
  165. /// </summary>
  166. /// <param name="path">The path.</param>
  167. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  168. /// <returns>IEnumerable&lt;System.String&gt;.</returns>
  169. IEnumerable<string> GetDirectoryPaths(string path, bool recursive = false);
  170. /// <summary>
  171. /// Gets the file paths.
  172. /// </summary>
  173. /// <param name="path">The path.</param>
  174. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  175. /// <returns>IEnumerable&lt;System.String&gt;.</returns>
  176. IEnumerable<string> GetFilePaths(string path, bool recursive = false);
  177. IEnumerable<string> GetFilePaths(string path, string[] extensions, bool enableCaseSensitiveExtensions, bool recursive);
  178. /// <summary>
  179. /// Gets the file system entry paths.
  180. /// </summary>
  181. /// <param name="path">The path.</param>
  182. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  183. /// <returns>IEnumerable&lt;System.String&gt;.</returns>
  184. IEnumerable<string> GetFileSystemEntryPaths(string path, bool recursive = false);
  185. void SetHidden(string path, bool isHidden);
  186. void SetReadOnly(string path, bool readOnly);
  187. void SetAttributes(string path, bool isHidden, bool readOnly);
  188. List<FileSystemMetadata> GetDrives();
  189. void SetExecutable(string path);
  190. }
  191. //TODO Investigate if can be replaced by the one from System.IO ?
  192. public enum FileOpenMode
  193. {
  194. //
  195. // Summary:
  196. // Specifies that the operating system should create a new file. This requires System.Security.Permissions.FileIOPermissionAccess.Write
  197. // permission. If the file already exists, an System.IO.IOException exception is
  198. // thrown.
  199. CreateNew = 1,
  200. //
  201. // Summary:
  202. // Specifies that the operating system should create a new file. If the file already
  203. // exists, it will be overwritten. This requires System.Security.Permissions.FileIOPermissionAccess.Write
  204. // permission. FileMode.Create is equivalent to requesting that if the file does
  205. // not exist, use System.IO.FileMode.CreateNew; otherwise, use System.IO.FileMode.Truncate.
  206. // If the file already exists but is a hidden file, an System.UnauthorizedAccessException
  207. // exception is thrown.
  208. Create = 2,
  209. //
  210. // Summary:
  211. // Specifies that the operating system should open an existing file. The ability
  212. // to open the file is dependent on the value specified by the System.IO.FileAccess
  213. // enumeration. A System.IO.FileNotFoundException exception is thrown if the file
  214. // does not exist.
  215. Open = 3,
  216. //
  217. // Summary:
  218. // Specifies that the operating system should open a file if it exists; otherwise,
  219. // a new file should be created. If the file is opened with FileAccess.Read, System.Security.Permissions.FileIOPermissionAccess.Read
  220. // permission is required. If the file access is FileAccess.Write, System.Security.Permissions.FileIOPermissionAccess.Write
  221. // permission is required. If the file is opened with FileAccess.ReadWrite, both
  222. // System.Security.Permissions.FileIOPermissionAccess.Read and System.Security.Permissions.FileIOPermissionAccess.Write
  223. // permissions are required.
  224. OpenOrCreate = 4
  225. }
  226. public enum FileAccessMode
  227. {
  228. //
  229. // Summary:
  230. // Read access to the file. Data can be read from the file. Combine with Write for
  231. // read/write access.
  232. Read = 1,
  233. //
  234. // Summary:
  235. // Write access to the file. Data can be written to the file. Combine with Read
  236. // for read/write access.
  237. Write = 2
  238. }
  239. public enum FileShareMode
  240. {
  241. //
  242. // Summary:
  243. // Declines sharing of the current file. Any request to open the file (by this process
  244. // or another process) will fail until the file is closed.
  245. None = 0,
  246. //
  247. // Summary:
  248. // Allows subsequent opening of the file for reading. If this flag is not specified,
  249. // any request to open the file for reading (by this process or another process)
  250. // will fail until the file is closed. However, even if this flag is specified,
  251. // additional permissions might still be needed to access the file.
  252. Read = 1,
  253. //
  254. // Summary:
  255. // Allows subsequent opening of the file for writing. If this flag is not specified,
  256. // any request to open the file for writing (by this process or another process)
  257. // will fail until the file is closed. However, even if this flag is specified,
  258. // additional permissions might still be needed to access the file.
  259. Write = 2,
  260. //
  261. // Summary:
  262. // Allows subsequent opening of the file for reading or writing. If this flag is
  263. // not specified, any request to open the file for reading or writing (by this process
  264. // or another process) will fail until the file is closed. However, even if this
  265. // flag is specified, additional permissions might still be needed to access the
  266. // file.
  267. ReadWrite = 3
  268. }
  269. //
  270. // Summary:
  271. // Represents advanced options for creating a System.IO.FileStream object.
  272. [Flags]
  273. public enum FileOpenOptions
  274. {
  275. //
  276. // Summary:
  277. // Indicates that the system should write through any intermediate cache and go
  278. // directly to disk.
  279. WriteThrough = int.MinValue,
  280. //
  281. // Summary:
  282. // Indicates that no additional options should be used when creating a System.IO.FileStream
  283. // object.
  284. None = 0,
  285. //
  286. // Summary:
  287. // Indicates that a file is encrypted and can be decrypted only by using the same
  288. // user account used for encryption.
  289. Encrypted = 16384,
  290. //
  291. // Summary:
  292. // Indicates that a file is automatically deleted when it is no longer in use.
  293. DeleteOnClose = 67108864,
  294. //
  295. // Summary:
  296. // Indicates that the file is to be accessed sequentially from beginning to end.
  297. // The system can use this as a hint to optimize file caching. If an application
  298. // moves the file pointer for random access, optimum caching may not occur; however,
  299. // correct operation is still guaranteed.
  300. SequentialScan = 134217728,
  301. //
  302. // Summary:
  303. // Indicates that the file is accessed randomly. The system can use this as a hint
  304. // to optimize file caching.
  305. RandomAccess = 268435456,
  306. //
  307. // Summary:
  308. // Indicates that a file can be used for asynchronous reading and writing.
  309. Asynchronous = 1073741824
  310. }
  311. }