IFileSystem.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. string GetDirectoryName(string path);
  130. /// <summary>
  131. /// Gets the file name without extension.
  132. /// </summary>
  133. /// <param name="info">The information.</param>
  134. /// <returns>System.String.</returns>
  135. string GetFileNameWithoutExtension(FileSystemMetadata info);
  136. /// <summary>
  137. /// Gets the file name without extension.
  138. /// </summary>
  139. /// <param name="path">The path.</param>
  140. /// <returns>System.String.</returns>
  141. string GetFileNameWithoutExtension(string path);
  142. /// <summary>
  143. /// Determines whether [is path file] [the specified path].
  144. /// </summary>
  145. /// <param name="path">The path.</param>
  146. /// <returns><c>true</c> if [is path file] [the specified path]; otherwise, <c>false</c>.</returns>
  147. bool IsPathFile(string path);
  148. /// <summary>
  149. /// Deletes the file.
  150. /// </summary>
  151. /// <param name="path">The path.</param>
  152. void DeleteFile(string path);
  153. /// <summary>
  154. /// Deletes the directory.
  155. /// </summary>
  156. /// <param name="path">The path.</param>
  157. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  158. void DeleteDirectory(string path, bool recursive);
  159. /// <summary>
  160. /// Gets the directories.
  161. /// </summary>
  162. /// <param name="path">The path.</param>
  163. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  164. /// <returns>IEnumerable&lt;DirectoryInfo&gt;.</returns>
  165. IEnumerable<FileSystemMetadata> GetDirectories(string path, bool recursive = false);
  166. /// <summary>
  167. /// Gets the files.
  168. /// </summary>
  169. IEnumerable<FileSystemMetadata> GetFiles(string path, bool recursive = false);
  170. IEnumerable<FileSystemMetadata> GetFiles(string path, string [] extensions, bool enableCaseSensitiveExtensions, bool recursive);
  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. IEnumerable<string> GetFilePaths(string path, string[] extensions, bool enableCaseSensitiveExtensions, bool recursive);
  259. /// <summary>
  260. /// Gets the file system entry paths.
  261. /// </summary>
  262. /// <param name="path">The path.</param>
  263. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  264. /// <returns>IEnumerable&lt;System.String&gt;.</returns>
  265. IEnumerable<string> GetFileSystemEntryPaths(string path, bool recursive = false);
  266. void SetHidden(string path, bool isHidden);
  267. void SetReadOnly(string path, bool isHidden);
  268. char DirectorySeparatorChar { 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. public enum FileAccessMode
  308. {
  309. //
  310. // Summary:
  311. // Read access to the file. Data can be read from the file. Combine with Write for
  312. // read/write access.
  313. Read = 1,
  314. //
  315. // Summary:
  316. // Write access to the file. Data can be written to the file. Combine with Read
  317. // for read/write access.
  318. Write = 2
  319. }
  320. public enum FileShareMode
  321. {
  322. //
  323. // Summary:
  324. // Declines sharing of the current file. Any request to open the file (by this process
  325. // or another process) will fail until the file is closed.
  326. None = 0,
  327. //
  328. // Summary:
  329. // Allows subsequent opening of the file for reading. If this flag is not specified,
  330. // any request to open the file for reading (by this process or another process)
  331. // will fail until the file is closed. However, even if this flag is specified,
  332. // additional permissions might still be needed to access the file.
  333. Read = 1,
  334. //
  335. // Summary:
  336. // Allows subsequent opening of the file for writing. If this flag is not specified,
  337. // any request to open the file for writing (by this process or another process)
  338. // will fail until the file is closed. However, even if this flag is specified,
  339. // additional permissions might still be needed to access the file.
  340. Write = 2,
  341. //
  342. // Summary:
  343. // Allows subsequent opening of the file for reading or writing. If this flag is
  344. // not specified, any request to open the file for reading or writing (by this process
  345. // or another process) will fail until the file is closed. However, even if this
  346. // flag is specified, additional permissions might still be needed to access the
  347. // file.
  348. ReadWrite = 3
  349. }
  350. }