IFileSystem.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. /// <param name="path">The path.</param>
  169. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  170. /// <returns>IEnumerable&lt;FileInfo&gt;.</returns>
  171. IEnumerable<FileSystemMetadata> GetFiles(string path, bool recursive = false);
  172. /// <summary>
  173. /// Gets the file system entries.
  174. /// </summary>
  175. /// <param name="path">The path.</param>
  176. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  177. /// <returns>IEnumerable&lt;FileSystemMetadata&gt;.</returns>
  178. IEnumerable<FileSystemMetadata> GetFileSystemEntries(string path, bool recursive = false);
  179. /// <summary>
  180. /// Creates the directory.
  181. /// </summary>
  182. /// <param name="path">The path.</param>
  183. void CreateDirectory(string path);
  184. /// <summary>
  185. /// Copies the file.
  186. /// </summary>
  187. /// <param name="source">The source.</param>
  188. /// <param name="target">The target.</param>
  189. /// <param name="overwrite">if set to <c>true</c> [overwrite].</param>
  190. void CopyFile(string source, string target, bool overwrite);
  191. /// <summary>
  192. /// Moves the file.
  193. /// </summary>
  194. /// <param name="source">The source.</param>
  195. /// <param name="target">The target.</param>
  196. void MoveFile(string source, string target);
  197. /// <summary>
  198. /// Moves the directory.
  199. /// </summary>
  200. /// <param name="source">The source.</param>
  201. /// <param name="target">The target.</param>
  202. void MoveDirectory(string source, string target);
  203. /// <summary>
  204. /// Directories the exists.
  205. /// </summary>
  206. /// <param name="path">The path.</param>
  207. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
  208. bool DirectoryExists(string path);
  209. /// <summary>
  210. /// Files the exists.
  211. /// </summary>
  212. /// <param name="path">The path.</param>
  213. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
  214. bool FileExists(string path);
  215. /// <summary>
  216. /// Reads all text.
  217. /// </summary>
  218. /// <param name="path">The path.</param>
  219. /// <returns>System.String.</returns>
  220. string ReadAllText(string path);
  221. byte[] ReadAllBytes(string path);
  222. void WriteAllBytes(string path, byte[] bytes);
  223. /// <summary>
  224. /// Writes all text.
  225. /// </summary>
  226. /// <param name="path">The path.</param>
  227. /// <param name="text">The text.</param>
  228. void WriteAllText(string path, string text);
  229. /// <summary>
  230. /// Writes all text.
  231. /// </summary>
  232. /// <param name="path">The path.</param>
  233. /// <param name="text">The text.</param>
  234. /// <param name="encoding">The encoding.</param>
  235. void WriteAllText(string path, string text, Encoding encoding);
  236. /// <summary>
  237. /// Reads all text.
  238. /// </summary>
  239. /// <param name="path">The path.</param>
  240. /// <param name="encoding">The encoding.</param>
  241. /// <returns>System.String.</returns>
  242. string ReadAllText(string path, Encoding encoding);
  243. string[] ReadAllLines(string path);
  244. void WriteAllLines(string path, IEnumerable<string> lines);
  245. /// <summary>
  246. /// Gets the directory paths.
  247. /// </summary>
  248. /// <param name="path">The path.</param>
  249. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  250. /// <returns>IEnumerable&lt;System.String&gt;.</returns>
  251. IEnumerable<string> GetDirectoryPaths(string path, bool recursive = false);
  252. /// <summary>
  253. /// Gets the file paths.
  254. /// </summary>
  255. /// <param name="path">The path.</param>
  256. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  257. /// <returns>IEnumerable&lt;System.String&gt;.</returns>
  258. IEnumerable<string> GetFilePaths(string path, bool recursive = false);
  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. char PathSeparator { get; }
  270. string GetFullPath(string path);
  271. List<FileSystemMetadata> GetDrives();
  272. void SetExecutable(string path);
  273. }
  274. public enum FileOpenMode
  275. {
  276. //
  277. // Summary:
  278. // Specifies that the operating system should create a new file. This requires System.Security.Permissions.FileIOPermissionAccess.Write
  279. // permission. If the file already exists, an System.IO.IOException exception is
  280. // thrown.
  281. CreateNew = 1,
  282. //
  283. // Summary:
  284. // Specifies that the operating system should create a new file. If the file already
  285. // exists, it will be overwritten. This requires System.Security.Permissions.FileIOPermissionAccess.Write
  286. // permission. FileMode.Create is equivalent to requesting that if the file does
  287. // not exist, use System.IO.FileMode.CreateNew; otherwise, use System.IO.FileMode.Truncate.
  288. // If the file already exists but is a hidden file, an System.UnauthorizedAccessException
  289. // exception is thrown.
  290. Create = 2,
  291. //
  292. // Summary:
  293. // Specifies that the operating system should open an existing file. The ability
  294. // to open the file is dependent on the value specified by the System.IO.FileAccess
  295. // enumeration. A System.IO.FileNotFoundException exception is thrown if the file
  296. // does not exist.
  297. Open = 3,
  298. //
  299. // Summary:
  300. // Specifies that the operating system should open a file if it exists; otherwise,
  301. // a new file should be created. If the file is opened with FileAccess.Read, System.Security.Permissions.FileIOPermissionAccess.Read
  302. // permission is required. If the file access is FileAccess.Write, System.Security.Permissions.FileIOPermissionAccess.Write
  303. // permission is required. If the file is opened with FileAccess.ReadWrite, both
  304. // System.Security.Permissions.FileIOPermissionAccess.Read and System.Security.Permissions.FileIOPermissionAccess.Write
  305. // permissions are required.
  306. OpenOrCreate = 4,
  307. //
  308. // Summary:
  309. // Specifies that the operating system should open an existing file. When the file
  310. // is opened, it should be truncated so that its size is zero bytes. This requires
  311. // System.Security.Permissions.FileIOPermissionAccess.Write permission. Attempts
  312. // to read from a file opened with FileMode.Truncate cause an System.ArgumentException
  313. // exception.
  314. Truncate = 5,
  315. //
  316. // Summary:
  317. // Opens the file if it exists and seeks to the end of the file, or creates a new
  318. // file. This requires System.Security.Permissions.FileIOPermissionAccess.Append
  319. // permission. FileMode.Append can be used only in conjunction with FileAccess.Write.
  320. // Trying to seek to a position before the end of the file throws an System.IO.IOException
  321. // exception, and any attempt to read fails and throws a System.NotSupportedException
  322. // exception.
  323. Append = 6
  324. }
  325. public enum FileAccessMode
  326. {
  327. //
  328. // Summary:
  329. // Read access to the file. Data can be read from the file. Combine with Write for
  330. // read/write access.
  331. Read = 1,
  332. //
  333. // Summary:
  334. // Write access to the file. Data can be written to the file. Combine with Read
  335. // for read/write access.
  336. Write = 2,
  337. //
  338. // Summary:
  339. // Read and write access to the file. Data can be written to and read from the file.
  340. ReadWrite = 3
  341. }
  342. public enum FileShareMode
  343. {
  344. //
  345. // Summary:
  346. // Declines sharing of the current file. Any request to open the file (by this process
  347. // or another process) will fail until the file is closed.
  348. None = 0,
  349. //
  350. // Summary:
  351. // Allows subsequent opening of the file for reading. If this flag is not specified,
  352. // any request to open the file for reading (by this process or another process)
  353. // will fail until the file is closed. However, even if this flag is specified,
  354. // additional permissions might still be needed to access the file.
  355. Read = 1,
  356. //
  357. // Summary:
  358. // Allows subsequent opening of the file for writing. If this flag is not specified,
  359. // any request to open the file for writing (by this process or another process)
  360. // will fail until the file is closed. However, even if this flag is specified,
  361. // additional permissions might still be needed to access the file.
  362. Write = 2,
  363. //
  364. // Summary:
  365. // Allows subsequent opening of the file for reading or writing. If this flag is
  366. // not specified, any request to open the file for reading or writing (by this process
  367. // or another process) will fail until the file is closed. However, even if this
  368. // flag is specified, additional permissions might still be needed to access the
  369. // file.
  370. ReadWrite = 3
  371. }
  372. }