IFileSystem.cs 18 KB

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