IFileSystem.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. bool AreEqual(string path1, string path2);
  109. /// <summary>
  110. /// Determines whether [contains sub path] [the specified parent path].
  111. /// </summary>
  112. /// <param name="parentPath">The parent path.</param>
  113. /// <param name="path">The path.</param>
  114. /// <returns><c>true</c> if [contains sub path] [the specified parent path]; otherwise, <c>false</c>.</returns>
  115. bool ContainsSubPath(string parentPath, string path);
  116. /// <summary>
  117. /// Determines whether [is root path] [the specified path].
  118. /// </summary>
  119. /// <param name="path">The path.</param>
  120. /// <returns><c>true</c> if [is root path] [the specified path]; otherwise, <c>false</c>.</returns>
  121. bool IsRootPath(string path);
  122. /// <summary>
  123. /// Normalizes the path.
  124. /// </summary>
  125. /// <param name="path">The path.</param>
  126. /// <returns>System.String.</returns>
  127. string NormalizePath(string path);
  128. /// <summary>
  129. /// Gets the file name without extension.
  130. /// </summary>
  131. /// <param name="info">The information.</param>
  132. /// <returns>System.String.</returns>
  133. string GetFileNameWithoutExtension(FileSystemMetadata info);
  134. /// <summary>
  135. /// Gets the file name without extension.
  136. /// </summary>
  137. /// <param name="path">The path.</param>
  138. /// <returns>System.String.</returns>
  139. string GetFileNameWithoutExtension(string path);
  140. /// <summary>
  141. /// Determines whether [is path file] [the specified path].
  142. /// </summary>
  143. /// <param name="path">The path.</param>
  144. /// <returns><c>true</c> if [is path file] [the specified path]; otherwise, <c>false</c>.</returns>
  145. bool IsPathFile(string path);
  146. /// <summary>
  147. /// Deletes the file.
  148. /// </summary>
  149. /// <param name="path">The path.</param>
  150. void DeleteFile(string path);
  151. /// <summary>
  152. /// Deletes the directory.
  153. /// </summary>
  154. /// <param name="path">The path.</param>
  155. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  156. void DeleteDirectory(string path, bool recursive);
  157. /// <summary>
  158. /// Gets the directories.
  159. /// </summary>
  160. /// <param name="path">The path.</param>
  161. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  162. /// <returns>IEnumerable&lt;DirectoryInfo&gt;.</returns>
  163. IEnumerable<FileSystemMetadata> GetDirectories(string path, bool recursive = false);
  164. /// <summary>
  165. /// Gets the files.
  166. /// </summary>
  167. /// <param name="path">The path.</param>
  168. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  169. /// <returns>IEnumerable&lt;FileInfo&gt;.</returns>
  170. IEnumerable<FileSystemMetadata> GetFiles(string path, bool recursive = false);
  171. /// <summary>
  172. /// Gets the file system entries.
  173. /// </summary>
  174. /// <param name="path">The path.</param>
  175. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  176. /// <returns>IEnumerable&lt;FileSystemMetadata&gt;.</returns>
  177. IEnumerable<FileSystemMetadata> GetFileSystemEntries(string path, bool recursive = false);
  178. /// <summary>
  179. /// Creates the directory.
  180. /// </summary>
  181. /// <param name="path">The path.</param>
  182. void CreateDirectory(string path);
  183. /// <summary>
  184. /// Copies the file.
  185. /// </summary>
  186. /// <param name="source">The source.</param>
  187. /// <param name="target">The target.</param>
  188. /// <param name="overwrite">if set to <c>true</c> [overwrite].</param>
  189. void CopyFile(string source, string target, bool overwrite);
  190. /// <summary>
  191. /// Moves the file.
  192. /// </summary>
  193. /// <param name="source">The source.</param>
  194. /// <param name="target">The target.</param>
  195. void MoveFile(string source, string target);
  196. /// <summary>
  197. /// Moves the directory.
  198. /// </summary>
  199. /// <param name="source">The source.</param>
  200. /// <param name="target">The target.</param>
  201. void MoveDirectory(string source, string target);
  202. /// <summary>
  203. /// Directories the exists.
  204. /// </summary>
  205. /// <param name="path">The path.</param>
  206. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
  207. bool DirectoryExists(string path);
  208. /// <summary>
  209. /// Files the exists.
  210. /// </summary>
  211. /// <param name="path">The path.</param>
  212. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
  213. bool FileExists(string path);
  214. /// <summary>
  215. /// Reads all text.
  216. /// </summary>
  217. /// <param name="path">The path.</param>
  218. /// <returns>System.String.</returns>
  219. string ReadAllText(string path);
  220. byte[] ReadAllBytes(string path);
  221. void WriteAllBytes(string path, byte[] bytes);
  222. /// <summary>
  223. /// Writes all text.
  224. /// </summary>
  225. /// <param name="path">The path.</param>
  226. /// <param name="text">The text.</param>
  227. void WriteAllText(string path, string text);
  228. /// <summary>
  229. /// Writes all text.
  230. /// </summary>
  231. /// <param name="path">The path.</param>
  232. /// <param name="text">The text.</param>
  233. /// <param name="encoding">The encoding.</param>
  234. void WriteAllText(string path, string text, Encoding encoding);
  235. /// <summary>
  236. /// Reads all text.
  237. /// </summary>
  238. /// <param name="path">The path.</param>
  239. /// <param name="encoding">The encoding.</param>
  240. /// <returns>System.String.</returns>
  241. string ReadAllText(string path, Encoding encoding);
  242. string[] ReadAllLines(string path);
  243. void WriteAllLines(string path, IEnumerable<string> lines);
  244. /// <summary>
  245. /// Gets the directory paths.
  246. /// </summary>
  247. /// <param name="path">The path.</param>
  248. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  249. /// <returns>IEnumerable&lt;System.String&gt;.</returns>
  250. IEnumerable<string> GetDirectoryPaths(string path, bool recursive = false);
  251. /// <summary>
  252. /// Gets the file paths.
  253. /// </summary>
  254. /// <param name="path">The path.</param>
  255. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  256. /// <returns>IEnumerable&lt;System.String&gt;.</returns>
  257. IEnumerable<string> GetFilePaths(string path, bool recursive = false);
  258. /// <summary>
  259. /// Gets the file system entry paths.
  260. /// </summary>
  261. /// <param name="path">The path.</param>
  262. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  263. /// <returns>IEnumerable&lt;System.String&gt;.</returns>
  264. IEnumerable<string> GetFileSystemEntryPaths(string path, bool recursive = false);
  265. void SetHidden(string path, bool isHidden);
  266. void SetReadOnly(string path, bool isHidden);
  267. char DirectorySeparatorChar { get; }
  268. char PathSeparator { get; }
  269. string GetFullPath(string path);
  270. List<FileSystemMetadata> GetDrives();
  271. void SetExecutable(string path);
  272. }
  273. public enum FileOpenMode
  274. {
  275. //
  276. // Summary:
  277. // Specifies that the operating system should create a new file. This requires System.Security.Permissions.FileIOPermissionAccess.Write
  278. // permission. If the file already exists, an System.IO.IOException exception is
  279. // thrown.
  280. CreateNew = 1,
  281. //
  282. // Summary:
  283. // Specifies that the operating system should create a new file. If the file already
  284. // exists, it will be overwritten. This requires System.Security.Permissions.FileIOPermissionAccess.Write
  285. // permission. FileMode.Create is equivalent to requesting that if the file does
  286. // not exist, use System.IO.FileMode.CreateNew; otherwise, use System.IO.FileMode.Truncate.
  287. // If the file already exists but is a hidden file, an System.UnauthorizedAccessException
  288. // exception is thrown.
  289. Create = 2,
  290. //
  291. // Summary:
  292. // Specifies that the operating system should open an existing file. The ability
  293. // to open the file is dependent on the value specified by the System.IO.FileAccess
  294. // enumeration. A System.IO.FileNotFoundException exception is thrown if the file
  295. // does not exist.
  296. Open = 3,
  297. //
  298. // Summary:
  299. // Specifies that the operating system should open a file if it exists; otherwise,
  300. // a new file should be created. If the file is opened with FileAccess.Read, System.Security.Permissions.FileIOPermissionAccess.Read
  301. // permission is required. If the file access is FileAccess.Write, System.Security.Permissions.FileIOPermissionAccess.Write
  302. // permission is required. If the file is opened with FileAccess.ReadWrite, both
  303. // System.Security.Permissions.FileIOPermissionAccess.Read and System.Security.Permissions.FileIOPermissionAccess.Write
  304. // permissions are required.
  305. OpenOrCreate = 4,
  306. //
  307. // Summary:
  308. // Specifies that the operating system should open an existing file. When the file
  309. // is opened, it should be truncated so that its size is zero bytes. This requires
  310. // System.Security.Permissions.FileIOPermissionAccess.Write permission. Attempts
  311. // to read from a file opened with FileMode.Truncate cause an System.ArgumentException
  312. // exception.
  313. Truncate = 5,
  314. //
  315. // Summary:
  316. // Opens the file if it exists and seeks to the end of the file, or creates a new
  317. // file. This requires System.Security.Permissions.FileIOPermissionAccess.Append
  318. // permission. FileMode.Append can be used only in conjunction with FileAccess.Write.
  319. // Trying to seek to a position before the end of the file throws an System.IO.IOException
  320. // exception, and any attempt to read fails and throws a System.NotSupportedException
  321. // exception.
  322. Append = 6
  323. }
  324. public enum FileAccessMode
  325. {
  326. //
  327. // Summary:
  328. // Read access to the file. Data can be read from the file. Combine with Write for
  329. // read/write access.
  330. Read = 1,
  331. //
  332. // Summary:
  333. // Write access to the file. Data can be written to the file. Combine with Read
  334. // for read/write access.
  335. Write = 2,
  336. //
  337. // Summary:
  338. // Read and write access to the file. Data can be written to and read from the file.
  339. ReadWrite = 3
  340. }
  341. public enum FileShareMode
  342. {
  343. //
  344. // Summary:
  345. // Declines sharing of the current file. Any request to open the file (by this process
  346. // or another process) will fail until the file is closed.
  347. None = 0,
  348. //
  349. // Summary:
  350. // Allows subsequent opening of the file for reading. If this flag is not specified,
  351. // any request to open the file for reading (by this process or another process)
  352. // will fail until the file is closed. However, even if this flag is specified,
  353. // additional permissions might still be needed to access the file.
  354. Read = 1,
  355. //
  356. // Summary:
  357. // Allows subsequent opening of the file for writing. If this flag is not specified,
  358. // any request to open the file for writing (by this process or another process)
  359. // will fail until the file is closed. However, even if this flag is specified,
  360. // additional permissions might still be needed to access the file.
  361. Write = 2,
  362. //
  363. // Summary:
  364. // Allows subsequent opening of the file for reading or writing. If this flag is
  365. // not specified, any request to open the file for reading or writing (by this process
  366. // or another process) will fail until the file is closed. However, even if this
  367. // flag is specified, additional permissions might still be needed to access the
  368. // file.
  369. ReadWrite = 3
  370. }
  371. }