IFileSystem.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. string[] ReadAllLines(string path);
  242. void WriteAllLines(string path, IEnumerable<string> lines);
  243. /// <summary>
  244. /// Gets the directory paths.
  245. /// </summary>
  246. /// <param name="path">The path.</param>
  247. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  248. /// <returns>IEnumerable&lt;System.String&gt;.</returns>
  249. IEnumerable<string> GetDirectoryPaths(string path, bool recursive = false);
  250. /// <summary>
  251. /// Gets the file paths.
  252. /// </summary>
  253. /// <param name="path">The path.</param>
  254. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  255. /// <returns>IEnumerable&lt;System.String&gt;.</returns>
  256. IEnumerable<string> GetFilePaths(string path, bool recursive = false);
  257. /// <summary>
  258. /// Gets the file system entry paths.
  259. /// </summary>
  260. /// <param name="path">The path.</param>
  261. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  262. /// <returns>IEnumerable&lt;System.String&gt;.</returns>
  263. IEnumerable<string> GetFileSystemEntryPaths(string path, bool recursive = false);
  264. void SetHidden(string path, bool isHidden);
  265. void SetReadOnly(string path, bool isHidden);
  266. char DirectorySeparatorChar { get; }
  267. char PathSeparator { get; }
  268. string GetFullPath(string path);
  269. List<FileSystemMetadata> GetDrives();
  270. void SetExecutable(string path);
  271. }
  272. public enum FileOpenMode
  273. {
  274. //
  275. // Summary:
  276. // Specifies that the operating system should create a new file. This requires System.Security.Permissions.FileIOPermissionAccess.Write
  277. // permission. If the file already exists, an System.IO.IOException exception is
  278. // thrown.
  279. CreateNew = 1,
  280. //
  281. // Summary:
  282. // Specifies that the operating system should create a new file. If the file already
  283. // exists, it will be overwritten. This requires System.Security.Permissions.FileIOPermissionAccess.Write
  284. // permission. FileMode.Create is equivalent to requesting that if the file does
  285. // not exist, use System.IO.FileMode.CreateNew; otherwise, use System.IO.FileMode.Truncate.
  286. // If the file already exists but is a hidden file, an System.UnauthorizedAccessException
  287. // exception is thrown.
  288. Create = 2,
  289. //
  290. // Summary:
  291. // Specifies that the operating system should open an existing file. The ability
  292. // to open the file is dependent on the value specified by the System.IO.FileAccess
  293. // enumeration. A System.IO.FileNotFoundException exception is thrown if the file
  294. // does not exist.
  295. Open = 3,
  296. //
  297. // Summary:
  298. // Specifies that the operating system should open a file if it exists; otherwise,
  299. // a new file should be created. If the file is opened with FileAccess.Read, System.Security.Permissions.FileIOPermissionAccess.Read
  300. // permission is required. If the file access is FileAccess.Write, System.Security.Permissions.FileIOPermissionAccess.Write
  301. // permission is required. If the file is opened with FileAccess.ReadWrite, both
  302. // System.Security.Permissions.FileIOPermissionAccess.Read and System.Security.Permissions.FileIOPermissionAccess.Write
  303. // permissions are required.
  304. OpenOrCreate = 4,
  305. //
  306. // Summary:
  307. // Specifies that the operating system should open an existing file. When the file
  308. // is opened, it should be truncated so that its size is zero bytes. This requires
  309. // System.Security.Permissions.FileIOPermissionAccess.Write permission. Attempts
  310. // to read from a file opened with FileMode.Truncate cause an System.ArgumentException
  311. // exception.
  312. Truncate = 5,
  313. //
  314. // Summary:
  315. // Opens the file if it exists and seeks to the end of the file, or creates a new
  316. // file. This requires System.Security.Permissions.FileIOPermissionAccess.Append
  317. // permission. FileMode.Append can be used only in conjunction with FileAccess.Write.
  318. // Trying to seek to a position before the end of the file throws an System.IO.IOException
  319. // exception, and any attempt to read fails and throws a System.NotSupportedException
  320. // exception.
  321. Append = 6
  322. }
  323. public enum FileAccessMode
  324. {
  325. //
  326. // Summary:
  327. // Read access to the file. Data can be read from the file. Combine with Write for
  328. // read/write access.
  329. Read = 1,
  330. //
  331. // Summary:
  332. // Write access to the file. Data can be written to the file. Combine with Read
  333. // for read/write access.
  334. Write = 2,
  335. //
  336. // Summary:
  337. // Read and write access to the file. Data can be written to and read from the file.
  338. ReadWrite = 3
  339. }
  340. public enum FileShareMode
  341. {
  342. //
  343. // Summary:
  344. // Declines sharing of the current file. Any request to open the file (by this process
  345. // or another process) will fail until the file is closed.
  346. None = 0,
  347. //
  348. // Summary:
  349. // Allows subsequent opening of the file for reading. If this flag is not specified,
  350. // any request to open the file for reading (by this process or another process)
  351. // will fail until the file is closed. However, even if this flag is specified,
  352. // additional permissions might still be needed to access the file.
  353. Read = 1,
  354. //
  355. // Summary:
  356. // Allows subsequent opening of the file for writing. If this flag is not specified,
  357. // any request to open the file for writing (by this process or another process)
  358. // will fail until the file is closed. However, even if this flag is specified,
  359. // additional permissions might still be needed to access the file.
  360. Write = 2,
  361. //
  362. // Summary:
  363. // Allows subsequent opening of the file for reading or writing. If this flag is
  364. // not specified, any request to open the file for reading or writing (by this process
  365. // or another process) will fail until the file is closed. However, even if this
  366. // flag is specified, additional permissions might still be needed to access the
  367. // file.
  368. ReadWrite = 3
  369. }
  370. }