IFileSystem.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. /// <summary>
  13. /// Determines whether the specified filename is shortcut.
  14. /// </summary>
  15. /// <param name="filename">The filename.</param>
  16. /// <returns><c>true</c> if the specified filename is shortcut; otherwise, <c>false</c>.</returns>
  17. bool IsShortcut(string filename);
  18. /// <summary>
  19. /// Resolves the shortcut.
  20. /// </summary>
  21. /// <param name="filename">The filename.</param>
  22. /// <returns>System.String.</returns>
  23. string ResolveShortcut(string filename);
  24. /// <summary>
  25. /// Creates the shortcut.
  26. /// </summary>
  27. /// <param name="shortcutPath">The shortcut path.</param>
  28. /// <param name="target">The target.</param>
  29. void CreateShortcut(string shortcutPath, string target);
  30. /// <summary>
  31. /// Returns a <see cref="FileSystemMetadata"/> object for the specified file or directory path.
  32. /// </summary>
  33. /// <param name="path">A path to a file or directory.</param>
  34. /// <returns>A <see cref="FileSystemMetadata"/> object.</returns>
  35. /// <remarks>If the specified path points to a directory, the returned <see cref="FileSystemMetadata"/> object's
  36. /// <see cref="FileSystemMetadata.IsDirectory"/> property will be set to true and all other properties will reflect the properties of the directory.</remarks>
  37. FileSystemMetadata GetFileSystemInfo(string path);
  38. /// <summary>
  39. /// Returns a <see cref="FileSystemMetadata"/> object for the specified file path.
  40. /// </summary>
  41. /// <param name="path">A path to a file.</param>
  42. /// <returns>A <see cref="FileSystemMetadata"/> object.</returns>
  43. /// <remarks><para>If the specified path points to a directory, the returned <see cref="FileSystemMetadata"/> object's
  44. /// <see cref="FileSystemMetadata.IsDirectory"/> property and the <see cref="FileSystemMetadata.Exists"/> property will both be set to false.</para>
  45. /// <para>For automatic handling of files <b>and</b> directories, use <see cref="GetFileSystemInfo"/>.</para></remarks>
  46. FileSystemMetadata GetFileInfo(string path);
  47. /// <summary>
  48. /// Returns a <see cref="FileSystemMetadata"/> object for the specified directory path.
  49. /// </summary>
  50. /// <param name="path">A path to a directory.</param>
  51. /// <returns>A <see cref="FileSystemMetadata"/> object.</returns>
  52. /// <remarks><para>If the specified path points to a file, the returned <see cref="FileSystemMetadata"/> object's
  53. /// <see cref="FileSystemMetadata.IsDirectory"/> property will be set to true and the <see cref="FileSystemMetadata.Exists"/> property will be set to false.</para>
  54. /// <para>For automatic handling of files <b>and</b> directories, use <see cref="GetFileSystemInfo"/>.</para></remarks>
  55. FileSystemMetadata GetDirectoryInfo(string path);
  56. /// <summary>
  57. /// Gets the valid filename.
  58. /// </summary>
  59. /// <param name="filename">The filename.</param>
  60. /// <returns>System.String.</returns>
  61. string GetValidFilename(string filename);
  62. /// <summary>
  63. /// Gets the creation time UTC.
  64. /// </summary>
  65. /// <param name="info">The information.</param>
  66. /// <returns>DateTime.</returns>
  67. DateTime GetCreationTimeUtc(FileSystemMetadata info);
  68. /// <summary>
  69. /// Gets the creation time UTC.
  70. /// </summary>
  71. /// <param name="path">The path.</param>
  72. /// <returns>DateTime.</returns>
  73. DateTime GetCreationTimeUtc(string path);
  74. /// <summary>
  75. /// Gets the last write time UTC.
  76. /// </summary>
  77. /// <param name="info">The information.</param>
  78. /// <returns>DateTime.</returns>
  79. DateTime GetLastWriteTimeUtc(FileSystemMetadata info);
  80. /// <summary>
  81. /// Gets the last write time UTC.
  82. /// </summary>
  83. /// <param name="path">The path.</param>
  84. /// <returns>DateTime.</returns>
  85. DateTime GetLastWriteTimeUtc(string path);
  86. /// <summary>
  87. /// Gets the file stream.
  88. /// </summary>
  89. /// <param name="path">The path.</param>
  90. /// <param name="mode">The mode.</param>
  91. /// <param name="access">The access.</param>
  92. /// <param name="share">The share.</param>
  93. /// <param name="isAsync">if set to <c>true</c> [is asynchronous].</param>
  94. /// <returns>FileStream.</returns>
  95. Stream GetFileStream(string path, FileOpenMode mode, FileAccessMode access, FileShareMode share, bool isAsync = false);
  96. /// <summary>
  97. /// Opens the read.
  98. /// </summary>
  99. /// <param name="path">The path.</param>
  100. /// <returns>Stream.</returns>
  101. Stream OpenRead(String path);
  102. /// <summary>
  103. /// Swaps the files.
  104. /// </summary>
  105. /// <param name="file1">The file1.</param>
  106. /// <param name="file2">The file2.</param>
  107. void SwapFiles(string file1, string file2);
  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. /// Gets the file name without extension.
  135. /// </summary>
  136. /// <param name="path">The path.</param>
  137. /// <returns>System.String.</returns>
  138. string GetFileNameWithoutExtension(string path);
  139. /// <summary>
  140. /// Determines whether [is path file] [the specified path].
  141. /// </summary>
  142. /// <param name="path">The path.</param>
  143. /// <returns><c>true</c> if [is path file] [the specified path]; otherwise, <c>false</c>.</returns>
  144. bool IsPathFile(string path);
  145. /// <summary>
  146. /// Deletes the file.
  147. /// </summary>
  148. /// <param name="path">The path.</param>
  149. void DeleteFile(string path);
  150. /// <summary>
  151. /// Deletes the directory.
  152. /// </summary>
  153. /// <param name="path">The path.</param>
  154. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  155. void DeleteDirectory(string path, bool recursive);
  156. /// <summary>
  157. /// Gets the directories.
  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;DirectoryInfo&gt;.</returns>
  162. IEnumerable<FileSystemMetadata> GetDirectories(string path, bool recursive = false);
  163. /// <summary>
  164. /// Gets the files.
  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;FileInfo&gt;.</returns>
  169. IEnumerable<FileSystemMetadata> GetFiles(string path, bool recursive = false);
  170. /// <summary>
  171. /// Gets the file system entries.
  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;FileSystemMetadata&gt;.</returns>
  176. IEnumerable<FileSystemMetadata> GetFileSystemEntries(string path, bool recursive = false);
  177. /// <summary>
  178. /// Creates the directory.
  179. /// </summary>
  180. /// <param name="path">The path.</param>
  181. void CreateDirectory(string path);
  182. /// <summary>
  183. /// Copies the file.
  184. /// </summary>
  185. /// <param name="source">The source.</param>
  186. /// <param name="target">The target.</param>
  187. /// <param name="overwrite">if set to <c>true</c> [overwrite].</param>
  188. void CopyFile(string source, string target, bool overwrite);
  189. /// <summary>
  190. /// Moves the file.
  191. /// </summary>
  192. /// <param name="source">The source.</param>
  193. /// <param name="target">The target.</param>
  194. void MoveFile(string source, string target);
  195. /// <summary>
  196. /// Moves the directory.
  197. /// </summary>
  198. /// <param name="source">The source.</param>
  199. /// <param name="target">The target.</param>
  200. void MoveDirectory(string source, string target);
  201. /// <summary>
  202. /// Directories the exists.
  203. /// </summary>
  204. /// <param name="path">The path.</param>
  205. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
  206. bool DirectoryExists(string path);
  207. /// <summary>
  208. /// Files the exists.
  209. /// </summary>
  210. /// <param name="path">The path.</param>
  211. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
  212. bool FileExists(string path);
  213. /// <summary>
  214. /// Reads all text.
  215. /// </summary>
  216. /// <param name="path">The path.</param>
  217. /// <returns>System.String.</returns>
  218. string ReadAllText(string path);
  219. byte[] ReadAllBytes(string path);
  220. void WriteAllBytes(string path, byte[] bytes);
  221. /// <summary>
  222. /// Writes all text.
  223. /// </summary>
  224. /// <param name="path">The path.</param>
  225. /// <param name="text">The text.</param>
  226. void WriteAllText(string path, string text);
  227. /// <summary>
  228. /// Writes all text.
  229. /// </summary>
  230. /// <param name="path">The path.</param>
  231. /// <param name="text">The text.</param>
  232. /// <param name="encoding">The encoding.</param>
  233. void WriteAllText(string path, string text, Encoding encoding);
  234. /// <summary>
  235. /// Reads all text.
  236. /// </summary>
  237. /// <param name="path">The path.</param>
  238. /// <param name="encoding">The encoding.</param>
  239. /// <returns>System.String.</returns>
  240. string ReadAllText(string path, Encoding encoding);
  241. /// <summary>
  242. /// Gets the directory paths.
  243. /// </summary>
  244. /// <param name="path">The path.</param>
  245. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  246. /// <returns>IEnumerable&lt;System.String&gt;.</returns>
  247. IEnumerable<string> GetDirectoryPaths(string path, bool recursive = false);
  248. /// <summary>
  249. /// Gets the file paths.
  250. /// </summary>
  251. /// <param name="path">The path.</param>
  252. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  253. /// <returns>IEnumerable&lt;System.String&gt;.</returns>
  254. IEnumerable<string> GetFilePaths(string path, bool recursive = false);
  255. /// <summary>
  256. /// Gets the file system entry paths.
  257. /// </summary>
  258. /// <param name="path">The path.</param>
  259. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  260. /// <returns>IEnumerable&lt;System.String&gt;.</returns>
  261. IEnumerable<string> GetFileSystemEntryPaths(string path, bool recursive = false);
  262. void SetHidden(string path, bool isHidden);
  263. char DirectorySeparatorChar { get; }
  264. string GetFullPath(string path);
  265. List<FileSystemMetadata> GetDrives();
  266. }
  267. public enum FileOpenMode
  268. {
  269. //
  270. // Summary:
  271. // Specifies that the operating system should create a new file. This requires System.Security.Permissions.FileIOPermissionAccess.Write
  272. // permission. If the file already exists, an System.IO.IOException exception is
  273. // thrown.
  274. CreateNew = 1,
  275. //
  276. // Summary:
  277. // Specifies that the operating system should create a new file. If the file already
  278. // exists, it will be overwritten. This requires System.Security.Permissions.FileIOPermissionAccess.Write
  279. // permission. FileMode.Create is equivalent to requesting that if the file does
  280. // not exist, use System.IO.FileMode.CreateNew; otherwise, use System.IO.FileMode.Truncate.
  281. // If the file already exists but is a hidden file, an System.UnauthorizedAccessException
  282. // exception is thrown.
  283. Create = 2,
  284. //
  285. // Summary:
  286. // Specifies that the operating system should open an existing file. The ability
  287. // to open the file is dependent on the value specified by the System.IO.FileAccess
  288. // enumeration. A System.IO.FileNotFoundException exception is thrown if the file
  289. // does not exist.
  290. Open = 3,
  291. //
  292. // Summary:
  293. // Specifies that the operating system should open a file if it exists; otherwise,
  294. // a new file should be created. If the file is opened with FileAccess.Read, System.Security.Permissions.FileIOPermissionAccess.Read
  295. // permission is required. If the file access is FileAccess.Write, System.Security.Permissions.FileIOPermissionAccess.Write
  296. // permission is required. If the file is opened with FileAccess.ReadWrite, both
  297. // System.Security.Permissions.FileIOPermissionAccess.Read and System.Security.Permissions.FileIOPermissionAccess.Write
  298. // permissions are required.
  299. OpenOrCreate = 4,
  300. //
  301. // Summary:
  302. // Specifies that the operating system should open an existing file. When the file
  303. // is opened, it should be truncated so that its size is zero bytes. This requires
  304. // System.Security.Permissions.FileIOPermissionAccess.Write permission. Attempts
  305. // to read from a file opened with FileMode.Truncate cause an System.ArgumentException
  306. // exception.
  307. Truncate = 5,
  308. //
  309. // Summary:
  310. // Opens the file if it exists and seeks to the end of the file, or creates a new
  311. // file. This requires System.Security.Permissions.FileIOPermissionAccess.Append
  312. // permission. FileMode.Append can be used only in conjunction with FileAccess.Write.
  313. // Trying to seek to a position before the end of the file throws an System.IO.IOException
  314. // exception, and any attempt to read fails and throws a System.NotSupportedException
  315. // exception.
  316. Append = 6
  317. }
  318. [Flags]
  319. public enum FileAccessMode
  320. {
  321. //
  322. // Summary:
  323. // Read access to the file. Data can be read from the file. Combine with Write for
  324. // read/write access.
  325. Read = 1,
  326. //
  327. // Summary:
  328. // Write access to the file. Data can be written to the file. Combine with Read
  329. // for read/write access.
  330. Write = 2,
  331. //
  332. // Summary:
  333. // Read and write access to the file. Data can be written to and read from the file.
  334. ReadWrite = 3
  335. }
  336. [Flags]
  337. public enum FileShareMode
  338. {
  339. //
  340. // Summary:
  341. // Declines sharing of the current file. Any request to open the file (by this process
  342. // or another process) will fail until the file is closed.
  343. None = 0,
  344. //
  345. // Summary:
  346. // Allows subsequent opening of the file for reading. If this flag is not specified,
  347. // any request to open the file for reading (by this process or another process)
  348. // will fail until the file is closed. However, even if this flag is specified,
  349. // additional permissions might still be needed to access the file.
  350. Read = 1,
  351. //
  352. // Summary:
  353. // Allows subsequent opening of the file for writing. If this flag is not specified,
  354. // any request to open the file for writing (by this process or another process)
  355. // will fail until the file is closed. However, even if this flag is specified,
  356. // additional permissions might still be needed to access the file.
  357. Write = 2,
  358. //
  359. // Summary:
  360. // Allows subsequent opening of the file for reading or writing. If this flag is
  361. // not specified, any request to open the file for reading or writing (by this process
  362. // or another process) will fail until the file is closed. However, even if this
  363. // flag is specified, additional permissions might still be needed to access the
  364. // file.
  365. ReadWrite = 3,
  366. //
  367. // Summary:
  368. // Allows subsequent deleting of a file.
  369. Delete = 4,
  370. //
  371. // Summary:
  372. // Makes the file handle inheritable by child processes. This is not directly supported
  373. // by Win32.
  374. Inheritable = 16
  375. }
  376. }