IFileSystem.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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="GetFileSystemInfo"/>.</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="GetFileSystemInfo"/>.</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, FileOpenOptions fileOpenOptions);
  99. /// <summary>
  100. /// Opens the read.
  101. /// </summary>
  102. /// <param name="path">The path.</param>
  103. /// <returns>Stream.</returns>
  104. Stream OpenRead(string path);
  105. string DefaultDirectory { get; }
  106. /// <summary>
  107. /// Swaps the files.
  108. /// </summary>
  109. /// <param name="file1">The file1.</param>
  110. /// <param name="file2">The file2.</param>
  111. void SwapFiles(string file1, string file2);
  112. bool AreEqual(string path1, string path2);
  113. /// <summary>
  114. /// Determines whether [contains sub path] [the specified parent path].
  115. /// </summary>
  116. /// <param name="parentPath">The parent path.</param>
  117. /// <param name="path">The path.</param>
  118. /// <returns><c>true</c> if [contains sub path] [the specified parent path]; otherwise, <c>false</c>.</returns>
  119. bool ContainsSubPath(string parentPath, string path);
  120. /// <summary>
  121. /// Determines whether [is root path] [the specified path].
  122. /// </summary>
  123. /// <param name="path">The path.</param>
  124. /// <returns><c>true</c> if [is root path] [the specified path]; otherwise, <c>false</c>.</returns>
  125. bool IsRootPath(string path);
  126. /// <summary>
  127. /// Normalizes the path.
  128. /// </summary>
  129. /// <param name="path">The path.</param>
  130. /// <returns>System.String.</returns>
  131. string NormalizePath(string path);
  132. string GetDirectoryName(string path);
  133. /// <summary>
  134. /// Gets the file name without extension.
  135. /// </summary>
  136. /// <param name="info">The information.</param>
  137. /// <returns>System.String.</returns>
  138. string GetFileNameWithoutExtension(FileSystemMetadata info);
  139. /// <summary>
  140. /// Gets the file name without extension.
  141. /// </summary>
  142. /// <param name="path">The path.</param>
  143. /// <returns>System.String.</returns>
  144. string GetFileNameWithoutExtension(string path);
  145. /// <summary>
  146. /// Determines whether [is path file] [the specified path].
  147. /// </summary>
  148. /// <param name="path">The path.</param>
  149. /// <returns><c>true</c> if [is path file] [the specified path]; otherwise, <c>false</c>.</returns>
  150. bool IsPathFile(string path);
  151. /// <summary>
  152. /// Deletes the file.
  153. /// </summary>
  154. /// <param name="path">The path.</param>
  155. void DeleteFile(string path);
  156. /// <summary>
  157. /// Deletes the directory.
  158. /// </summary>
  159. /// <param name="path">The path.</param>
  160. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  161. void DeleteDirectory(string path, bool recursive);
  162. /// <summary>
  163. /// Gets the directories.
  164. /// </summary>
  165. /// <param name="path">The path.</param>
  166. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  167. /// <returns>IEnumerable&lt;DirectoryInfo&gt;.</returns>
  168. IEnumerable<FileSystemMetadata> GetDirectories(string path, bool recursive = false);
  169. /// <summary>
  170. /// Gets the files.
  171. /// </summary>
  172. IEnumerable<FileSystemMetadata> GetFiles(string path, bool recursive = false);
  173. IEnumerable<FileSystemMetadata> GetFiles(string path, string[] extensions, bool enableCaseSensitiveExtensions, bool recursive);
  174. /// <summary>
  175. /// Gets the file system entries.
  176. /// </summary>
  177. /// <param name="path">The path.</param>
  178. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  179. /// <returns>IEnumerable&lt;FileSystemMetadata&gt;.</returns>
  180. IEnumerable<FileSystemMetadata> GetFileSystemEntries(string path, bool recursive = false);
  181. /// <summary>
  182. /// Creates the directory.
  183. /// </summary>
  184. /// <param name="path">The path.</param>
  185. void CreateDirectory(string path);
  186. /// <summary>
  187. /// Copies the file.
  188. /// </summary>
  189. /// <param name="source">The source.</param>
  190. /// <param name="target">The target.</param>
  191. /// <param name="overwrite">if set to <c>true</c> [overwrite].</param>
  192. void CopyFile(string source, string target, bool overwrite);
  193. /// <summary>
  194. /// Moves the file.
  195. /// </summary>
  196. /// <param name="source">The source.</param>
  197. /// <param name="target">The target.</param>
  198. void MoveFile(string source, string target);
  199. /// <summary>
  200. /// Moves the directory.
  201. /// </summary>
  202. /// <param name="source">The source.</param>
  203. /// <param name="target">The target.</param>
  204. void MoveDirectory(string source, string target);
  205. /// <summary>
  206. /// Directories the exists.
  207. /// </summary>
  208. /// <param name="path">The path.</param>
  209. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
  210. bool DirectoryExists(string path);
  211. /// <summary>
  212. /// Files the exists.
  213. /// </summary>
  214. /// <param name="path">The path.</param>
  215. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
  216. bool FileExists(string path);
  217. /// <summary>
  218. /// Reads all text.
  219. /// </summary>
  220. /// <param name="path">The path.</param>
  221. /// <returns>System.String.</returns>
  222. string ReadAllText(string path);
  223. byte[] ReadAllBytes(string path);
  224. void WriteAllBytes(string path, byte[] bytes);
  225. /// <summary>
  226. /// Writes all text.
  227. /// </summary>
  228. /// <param name="path">The path.</param>
  229. /// <param name="text">The text.</param>
  230. void WriteAllText(string path, string text);
  231. /// <summary>
  232. /// Writes all text.
  233. /// </summary>
  234. /// <param name="path">The path.</param>
  235. /// <param name="text">The text.</param>
  236. /// <param name="encoding">The encoding.</param>
  237. void WriteAllText(string path, string text, Encoding encoding);
  238. /// <summary>
  239. /// Reads all text.
  240. /// </summary>
  241. /// <param name="path">The path.</param>
  242. /// <param name="encoding">The encoding.</param>
  243. /// <returns>System.String.</returns>
  244. string ReadAllText(string path, Encoding encoding);
  245. string[] ReadAllLines(string path);
  246. void WriteAllLines(string path, IEnumerable<string> lines);
  247. /// <summary>
  248. /// Gets the directory paths.
  249. /// </summary>
  250. /// <param name="path">The path.</param>
  251. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  252. /// <returns>IEnumerable&lt;System.String&gt;.</returns>
  253. IEnumerable<string> GetDirectoryPaths(string path, bool recursive = false);
  254. /// <summary>
  255. /// Gets the file paths.
  256. /// </summary>
  257. /// <param name="path">The path.</param>
  258. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  259. /// <returns>IEnumerable&lt;System.String&gt;.</returns>
  260. IEnumerable<string> GetFilePaths(string path, bool recursive = false);
  261. IEnumerable<string> GetFilePaths(string path, string[] extensions, bool enableCaseSensitiveExtensions, bool recursive);
  262. /// <summary>
  263. /// Gets the file system entry paths.
  264. /// </summary>
  265. /// <param name="path">The path.</param>
  266. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  267. /// <returns>IEnumerable&lt;System.String&gt;.</returns>
  268. IEnumerable<string> GetFileSystemEntryPaths(string path, bool recursive = false);
  269. void SetHidden(string path, bool isHidden);
  270. void SetReadOnly(string path, bool readOnly);
  271. void SetAttributes(string path, bool isHidden, bool readOnly);
  272. char DirectorySeparatorChar { get; }
  273. string GetFullPath(string path);
  274. List<FileSystemMetadata> GetDrives();
  275. void SetExecutable(string path);
  276. }
  277. //TODO Investigate if can be replaced by the one from System.IO ?
  278. public enum FileOpenMode
  279. {
  280. //
  281. // Summary:
  282. // Specifies that the operating system should create a new file. This requires System.Security.Permissions.FileIOPermissionAccess.Write
  283. // permission. If the file already exists, an System.IO.IOException exception is
  284. // thrown.
  285. CreateNew = 1,
  286. //
  287. // Summary:
  288. // Specifies that the operating system should create a new file. If the file already
  289. // exists, it will be overwritten. This requires System.Security.Permissions.FileIOPermissionAccess.Write
  290. // permission. FileMode.Create is equivalent to requesting that if the file does
  291. // not exist, use System.IO.FileMode.CreateNew; otherwise, use System.IO.FileMode.Truncate.
  292. // If the file already exists but is a hidden file, an System.UnauthorizedAccessException
  293. // exception is thrown.
  294. Create = 2,
  295. //
  296. // Summary:
  297. // Specifies that the operating system should open an existing file. The ability
  298. // to open the file is dependent on the value specified by the System.IO.FileAccess
  299. // enumeration. A System.IO.FileNotFoundException exception is thrown if the file
  300. // does not exist.
  301. Open = 3,
  302. //
  303. // Summary:
  304. // Specifies that the operating system should open a file if it exists; otherwise,
  305. // a new file should be created. If the file is opened with FileAccess.Read, System.Security.Permissions.FileIOPermissionAccess.Read
  306. // permission is required. If the file access is FileAccess.Write, System.Security.Permissions.FileIOPermissionAccess.Write
  307. // permission is required. If the file is opened with FileAccess.ReadWrite, both
  308. // System.Security.Permissions.FileIOPermissionAccess.Read and System.Security.Permissions.FileIOPermissionAccess.Write
  309. // permissions are required.
  310. OpenOrCreate = 4
  311. }
  312. public enum FileAccessMode
  313. {
  314. //
  315. // Summary:
  316. // Read access to the file. Data can be read from the file. Combine with Write for
  317. // read/write access.
  318. Read = 1,
  319. //
  320. // Summary:
  321. // Write access to the file. Data can be written to the file. Combine with Read
  322. // for read/write access.
  323. Write = 2
  324. }
  325. public enum FileShareMode
  326. {
  327. //
  328. // Summary:
  329. // Declines sharing of the current file. Any request to open the file (by this process
  330. // or another process) will fail until the file is closed.
  331. None = 0,
  332. //
  333. // Summary:
  334. // Allows subsequent opening of the file for reading. If this flag is not specified,
  335. // any request to open the file for reading (by this process or another process)
  336. // will fail until the file is closed. However, even if this flag is specified,
  337. // additional permissions might still be needed to access the file.
  338. Read = 1,
  339. //
  340. // Summary:
  341. // Allows subsequent opening of the file for writing. If this flag is not specified,
  342. // any request to open the file for writing (by this process or another process)
  343. // will fail until the file is closed. However, even if this flag is specified,
  344. // additional permissions might still be needed to access the file.
  345. Write = 2,
  346. //
  347. // Summary:
  348. // Allows subsequent opening of the file for reading or writing. If this flag is
  349. // not specified, any request to open the file for reading or writing (by this process
  350. // or another process) will fail until the file is closed. However, even if this
  351. // flag is specified, additional permissions might still be needed to access the
  352. // file.
  353. ReadWrite = 3
  354. }
  355. //
  356. // Summary:
  357. // Represents advanced options for creating a System.IO.FileStream object.
  358. [Flags]
  359. public enum FileOpenOptions
  360. {
  361. //
  362. // Summary:
  363. // Indicates that the system should write through any intermediate cache and go
  364. // directly to disk.
  365. WriteThrough = int.MinValue,
  366. //
  367. // Summary:
  368. // Indicates that no additional options should be used when creating a System.IO.FileStream
  369. // object.
  370. None = 0,
  371. //
  372. // Summary:
  373. // Indicates that a file is encrypted and can be decrypted only by using the same
  374. // user account used for encryption.
  375. Encrypted = 16384,
  376. //
  377. // Summary:
  378. // Indicates that a file is automatically deleted when it is no longer in use.
  379. DeleteOnClose = 67108864,
  380. //
  381. // Summary:
  382. // Indicates that the file is to be accessed sequentially from beginning to end.
  383. // The system can use this as a hint to optimize file caching. If an application
  384. // moves the file pointer for random access, optimum caching may not occur; however,
  385. // correct operation is still guaranteed.
  386. SequentialScan = 134217728,
  387. //
  388. // Summary:
  389. // Indicates that the file is accessed randomly. The system can use this as a hint
  390. // to optimize file caching.
  391. RandomAccess = 268435456,
  392. //
  393. // Summary:
  394. // Indicates that a file can be used for asynchronous reading and writing.
  395. Asynchronous = 1073741824
  396. }
  397. }