IFileSystem.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. /// <summary>
  98. /// Opens the read.
  99. /// </summary>
  100. /// <param name="path">The path.</param>
  101. /// <returns>Stream.</returns>
  102. Stream OpenRead(String path);
  103. /// <summary>
  104. /// Swaps the files.
  105. /// </summary>
  106. /// <param name="file1">The file1.</param>
  107. /// <param name="file2">The file2.</param>
  108. void SwapFiles(string file1, string file2);
  109. bool AreEqual(string path1, string path2);
  110. /// <summary>
  111. /// Determines whether [contains sub path] [the specified parent path].
  112. /// </summary>
  113. /// <param name="parentPath">The parent path.</param>
  114. /// <param name="path">The path.</param>
  115. /// <returns><c>true</c> if [contains sub path] [the specified parent path]; otherwise, <c>false</c>.</returns>
  116. bool ContainsSubPath(string parentPath, string path);
  117. /// <summary>
  118. /// Determines whether [is root path] [the specified path].
  119. /// </summary>
  120. /// <param name="path">The path.</param>
  121. /// <returns><c>true</c> if [is root path] [the specified path]; otherwise, <c>false</c>.</returns>
  122. bool IsRootPath(string path);
  123. /// <summary>
  124. /// Normalizes the path.
  125. /// </summary>
  126. /// <param name="path">The path.</param>
  127. /// <returns>System.String.</returns>
  128. string NormalizePath(string path);
  129. /// <summary>
  130. /// Gets the file name without extension.
  131. /// </summary>
  132. /// <param name="info">The information.</param>
  133. /// <returns>System.String.</returns>
  134. string GetFileNameWithoutExtension(FileSystemMetadata info);
  135. /// <summary>
  136. /// Gets the file name without extension.
  137. /// </summary>
  138. /// <param name="path">The path.</param>
  139. /// <returns>System.String.</returns>
  140. string GetFileNameWithoutExtension(string path);
  141. /// <summary>
  142. /// Determines whether [is path file] [the specified path].
  143. /// </summary>
  144. /// <param name="path">The path.</param>
  145. /// <returns><c>true</c> if [is path file] [the specified path]; otherwise, <c>false</c>.</returns>
  146. bool IsPathFile(string path);
  147. /// <summary>
  148. /// Deletes the file.
  149. /// </summary>
  150. /// <param name="path">The path.</param>
  151. void DeleteFile(string path);
  152. /// <summary>
  153. /// Deletes the directory.
  154. /// </summary>
  155. /// <param name="path">The path.</param>
  156. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  157. void DeleteDirectory(string path, bool recursive);
  158. /// <summary>
  159. /// Gets the directories.
  160. /// </summary>
  161. /// <param name="path">The path.</param>
  162. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  163. /// <returns>IEnumerable&lt;DirectoryInfo&gt;.</returns>
  164. IEnumerable<FileSystemMetadata> GetDirectories(string path, bool recursive = false);
  165. /// <summary>
  166. /// Gets the files.
  167. /// </summary>
  168. IEnumerable<FileSystemMetadata> GetFiles(string path, bool recursive = false);
  169. IEnumerable<FileSystemMetadata> GetFiles(string path, string [] extensions, bool enableCaseSensitiveExtensions, bool recursive);
  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. IEnumerable<string> GetFilePaths(string path, string[] extensions, bool enableCaseSensitiveExtensions, bool recursive);
  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. 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. public enum FileAccessMode
  307. {
  308. //
  309. // Summary:
  310. // Read access to the file. Data can be read from the file. Combine with Write for
  311. // read/write access.
  312. Read = 1,
  313. //
  314. // Summary:
  315. // Write access to the file. Data can be written to the file. Combine with Read
  316. // for read/write access.
  317. Write = 2
  318. }
  319. public enum FileShareMode
  320. {
  321. //
  322. // Summary:
  323. // Declines sharing of the current file. Any request to open the file (by this process
  324. // or another process) will fail until the file is closed.
  325. None = 0,
  326. //
  327. // Summary:
  328. // Allows subsequent opening of the file for reading. If this flag is not specified,
  329. // any request to open the file for reading (by this process or another process)
  330. // will fail until the file is closed. However, even if this flag is specified,
  331. // additional permissions might still be needed to access the file.
  332. Read = 1,
  333. //
  334. // Summary:
  335. // Allows subsequent opening of the file for writing. If this flag is not specified,
  336. // any request to open the file for writing (by this process or another process)
  337. // will fail until the file is closed. However, even if this flag is specified,
  338. // additional permissions might still be needed to access the file.
  339. Write = 2,
  340. //
  341. // Summary:
  342. // Allows subsequent opening of the file for reading or writing. If this flag is
  343. // not specified, any request to open the file for reading or writing (by this process
  344. // or another process) will fail until the file is closed. However, even if this
  345. // flag is specified, additional permissions might still be needed to access the
  346. // file.
  347. ReadWrite = 3
  348. }
  349. }