ManagedFileSystem.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using MediaBrowser.Model.IO;
  7. using MediaBrowser.Model.Logging;
  8. namespace Emby.Common.Implementations.IO
  9. {
  10. /// <summary>
  11. /// Class ManagedFileSystem
  12. /// </summary>
  13. public class ManagedFileSystem : IFileSystem
  14. {
  15. protected ILogger Logger;
  16. private readonly bool _supportsAsyncFileStreams;
  17. private char[] _invalidFileNameChars;
  18. private readonly List<IShortcutHandler> _shortcutHandlers = new List<IShortcutHandler>();
  19. private bool EnableFileSystemRequestConcat = true;
  20. public ManagedFileSystem(ILogger logger, bool supportsAsyncFileStreams, bool enableManagedInvalidFileNameChars, bool enableFileSystemRequestConcat)
  21. {
  22. Logger = logger;
  23. _supportsAsyncFileStreams = supportsAsyncFileStreams;
  24. EnableFileSystemRequestConcat = enableFileSystemRequestConcat;
  25. SetInvalidFileNameChars(enableManagedInvalidFileNameChars);
  26. }
  27. public void AddShortcutHandler(IShortcutHandler handler)
  28. {
  29. _shortcutHandlers.Add(handler);
  30. }
  31. protected void SetInvalidFileNameChars(bool enableManagedInvalidFileNameChars)
  32. {
  33. if (enableManagedInvalidFileNameChars)
  34. {
  35. _invalidFileNameChars = Path.GetInvalidFileNameChars();
  36. }
  37. else
  38. {
  39. // GetInvalidFileNameChars is less restrictive in Linux/Mac than Windows, this mimic Windows behavior for mono under Linux/Mac.
  40. _invalidFileNameChars = new char[41] { '\x00', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07',
  41. '\x08', '\x09', '\x0A', '\x0B', '\x0C', '\x0D', '\x0E', '\x0F', '\x10', '\x11', '\x12',
  42. '\x13', '\x14', '\x15', '\x16', '\x17', '\x18', '\x19', '\x1A', '\x1B', '\x1C', '\x1D',
  43. '\x1E', '\x1F', '\x22', '\x3C', '\x3E', '\x7C', ':', '*', '?', '\\', '/' };
  44. }
  45. }
  46. public char DirectorySeparatorChar
  47. {
  48. get
  49. {
  50. return Path.DirectorySeparatorChar;
  51. }
  52. }
  53. public string GetFullPath(string path)
  54. {
  55. return Path.GetFullPath(path);
  56. }
  57. /// <summary>
  58. /// Determines whether the specified filename is shortcut.
  59. /// </summary>
  60. /// <param name="filename">The filename.</param>
  61. /// <returns><c>true</c> if the specified filename is shortcut; otherwise, <c>false</c>.</returns>
  62. /// <exception cref="System.ArgumentNullException">filename</exception>
  63. public virtual bool IsShortcut(string filename)
  64. {
  65. if (string.IsNullOrEmpty(filename))
  66. {
  67. throw new ArgumentNullException("filename");
  68. }
  69. var extension = Path.GetExtension(filename);
  70. return _shortcutHandlers.Any(i => string.Equals(extension, i.Extension, StringComparison.OrdinalIgnoreCase));
  71. }
  72. /// <summary>
  73. /// Resolves the shortcut.
  74. /// </summary>
  75. /// <param name="filename">The filename.</param>
  76. /// <returns>System.String.</returns>
  77. /// <exception cref="System.ArgumentNullException">filename</exception>
  78. public virtual string ResolveShortcut(string filename)
  79. {
  80. if (string.IsNullOrEmpty(filename))
  81. {
  82. throw new ArgumentNullException("filename");
  83. }
  84. var extension = Path.GetExtension(filename);
  85. var handler = _shortcutHandlers.FirstOrDefault(i => string.Equals(extension, i.Extension, StringComparison.OrdinalIgnoreCase));
  86. if (handler != null)
  87. {
  88. return handler.Resolve(filename);
  89. }
  90. return null;
  91. }
  92. /// <summary>
  93. /// Creates the shortcut.
  94. /// </summary>
  95. /// <param name="shortcutPath">The shortcut path.</param>
  96. /// <param name="target">The target.</param>
  97. /// <exception cref="System.ArgumentNullException">
  98. /// shortcutPath
  99. /// or
  100. /// target
  101. /// </exception>
  102. public void CreateShortcut(string shortcutPath, string target)
  103. {
  104. if (string.IsNullOrEmpty(shortcutPath))
  105. {
  106. throw new ArgumentNullException("shortcutPath");
  107. }
  108. if (string.IsNullOrEmpty(target))
  109. {
  110. throw new ArgumentNullException("target");
  111. }
  112. var extension = Path.GetExtension(shortcutPath);
  113. var handler = _shortcutHandlers.FirstOrDefault(i => string.Equals(extension, i.Extension, StringComparison.OrdinalIgnoreCase));
  114. if (handler != null)
  115. {
  116. handler.Create(shortcutPath, target);
  117. }
  118. else
  119. {
  120. throw new NotImplementedException();
  121. }
  122. }
  123. /// <summary>
  124. /// Returns a <see cref="FileSystemMetadata"/> object for the specified file or directory path.
  125. /// </summary>
  126. /// <param name="path">A path to a file or directory.</param>
  127. /// <returns>A <see cref="FileSystemMetadata"/> object.</returns>
  128. /// <remarks>If the specified path points to a directory, the returned <see cref="FileSystemMetadata"/> object's
  129. /// <see cref="FileSystemMetadata.IsDirectory"/> property will be set to true and all other properties will reflect the properties of the directory.</remarks>
  130. public FileSystemMetadata GetFileSystemInfo(string path)
  131. {
  132. if (string.IsNullOrEmpty(path))
  133. {
  134. throw new ArgumentNullException("path");
  135. }
  136. // Take a guess to try and avoid two file system hits, but we'll double-check by calling Exists
  137. if (Path.HasExtension(path))
  138. {
  139. var fileInfo = new FileInfo(path);
  140. if (fileInfo.Exists)
  141. {
  142. return GetFileSystemMetadata(fileInfo);
  143. }
  144. return GetFileSystemMetadata(new DirectoryInfo(path));
  145. }
  146. else
  147. {
  148. var fileInfo = new DirectoryInfo(path);
  149. if (fileInfo.Exists)
  150. {
  151. return GetFileSystemMetadata(fileInfo);
  152. }
  153. return GetFileSystemMetadata(new FileInfo(path));
  154. }
  155. }
  156. /// <summary>
  157. /// Returns a <see cref="FileSystemMetadata"/> object for the specified file path.
  158. /// </summary>
  159. /// <param name="path">A path to a file.</param>
  160. /// <returns>A <see cref="FileSystemMetadata"/> object.</returns>
  161. /// <remarks><para>If the specified path points to a directory, the returned <see cref="FileSystemMetadata"/> object's
  162. /// <see cref="FileSystemMetadata.IsDirectory"/> property and the <see cref="FileSystemMetadata.Exists"/> property will both be set to false.</para>
  163. /// <para>For automatic handling of files <b>and</b> directories, use <see cref="GetFileSystemInfo"/>.</para></remarks>
  164. public FileSystemMetadata GetFileInfo(string path)
  165. {
  166. if (string.IsNullOrEmpty(path))
  167. {
  168. throw new ArgumentNullException("path");
  169. }
  170. var fileInfo = new FileInfo(path);
  171. return GetFileSystemMetadata(fileInfo);
  172. }
  173. /// <summary>
  174. /// Returns a <see cref="FileSystemMetadata"/> object for the specified directory path.
  175. /// </summary>
  176. /// <param name="path">A path to a directory.</param>
  177. /// <returns>A <see cref="FileSystemMetadata"/> object.</returns>
  178. /// <remarks><para>If the specified path points to a file, the returned <see cref="FileSystemMetadata"/> object's
  179. /// <see cref="FileSystemMetadata.IsDirectory"/> property will be set to true and the <see cref="FileSystemMetadata.Exists"/> property will be set to false.</para>
  180. /// <para>For automatic handling of files <b>and</b> directories, use <see cref="GetFileSystemInfo"/>.</para></remarks>
  181. public FileSystemMetadata GetDirectoryInfo(string path)
  182. {
  183. if (string.IsNullOrEmpty(path))
  184. {
  185. throw new ArgumentNullException("path");
  186. }
  187. var fileInfo = new DirectoryInfo(path);
  188. return GetFileSystemMetadata(fileInfo);
  189. }
  190. private FileSystemMetadata GetFileSystemMetadata(FileSystemInfo info)
  191. {
  192. var result = new FileSystemMetadata();
  193. result.Exists = info.Exists;
  194. result.FullName = info.FullName;
  195. result.Extension = info.Extension;
  196. result.Name = info.Name;
  197. if (result.Exists)
  198. {
  199. var attributes = info.Attributes;
  200. result.IsDirectory = info is DirectoryInfo || (attributes & FileAttributes.Directory) == FileAttributes.Directory;
  201. result.IsHidden = (attributes & FileAttributes.Hidden) == FileAttributes.Hidden;
  202. result.IsReadOnly = (attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly;
  203. var fileInfo = info as FileInfo;
  204. if (fileInfo != null)
  205. {
  206. result.Length = fileInfo.Length;
  207. result.DirectoryName = fileInfo.DirectoryName;
  208. }
  209. result.CreationTimeUtc = GetCreationTimeUtc(info);
  210. result.LastWriteTimeUtc = GetLastWriteTimeUtc(info);
  211. }
  212. else
  213. {
  214. result.IsDirectory = info is DirectoryInfo;
  215. }
  216. return result;
  217. }
  218. /// <summary>
  219. /// The space char
  220. /// </summary>
  221. private const char SpaceChar = ' ';
  222. /// <summary>
  223. /// Takes a filename and removes invalid characters
  224. /// </summary>
  225. /// <param name="filename">The filename.</param>
  226. /// <returns>System.String.</returns>
  227. /// <exception cref="System.ArgumentNullException">filename</exception>
  228. public string GetValidFilename(string filename)
  229. {
  230. if (string.IsNullOrEmpty(filename))
  231. {
  232. throw new ArgumentNullException("filename");
  233. }
  234. var builder = new StringBuilder(filename);
  235. foreach (var c in _invalidFileNameChars)
  236. {
  237. builder = builder.Replace(c, SpaceChar);
  238. }
  239. return builder.ToString();
  240. }
  241. /// <summary>
  242. /// Gets the creation time UTC.
  243. /// </summary>
  244. /// <param name="info">The info.</param>
  245. /// <returns>DateTime.</returns>
  246. public DateTime GetCreationTimeUtc(FileSystemInfo info)
  247. {
  248. // This could throw an error on some file systems that have dates out of range
  249. try
  250. {
  251. return info.CreationTimeUtc;
  252. }
  253. catch (Exception ex)
  254. {
  255. Logger.ErrorException("Error determining CreationTimeUtc for {0}", ex, info.FullName);
  256. return DateTime.MinValue;
  257. }
  258. }
  259. /// <summary>
  260. /// Gets the creation time UTC.
  261. /// </summary>
  262. /// <param name="path">The path.</param>
  263. /// <returns>DateTime.</returns>
  264. public DateTime GetCreationTimeUtc(string path)
  265. {
  266. return GetCreationTimeUtc(GetFileSystemInfo(path));
  267. }
  268. public DateTime GetCreationTimeUtc(FileSystemMetadata info)
  269. {
  270. return info.CreationTimeUtc;
  271. }
  272. public DateTime GetLastWriteTimeUtc(FileSystemMetadata info)
  273. {
  274. return info.LastWriteTimeUtc;
  275. }
  276. /// <summary>
  277. /// Gets the creation time UTC.
  278. /// </summary>
  279. /// <param name="info">The info.</param>
  280. /// <returns>DateTime.</returns>
  281. public DateTime GetLastWriteTimeUtc(FileSystemInfo info)
  282. {
  283. // This could throw an error on some file systems that have dates out of range
  284. try
  285. {
  286. return info.LastWriteTimeUtc;
  287. }
  288. catch (Exception ex)
  289. {
  290. Logger.ErrorException("Error determining LastAccessTimeUtc for {0}", ex, info.FullName);
  291. return DateTime.MinValue;
  292. }
  293. }
  294. /// <summary>
  295. /// Gets the last write time UTC.
  296. /// </summary>
  297. /// <param name="path">The path.</param>
  298. /// <returns>DateTime.</returns>
  299. public DateTime GetLastWriteTimeUtc(string path)
  300. {
  301. return GetLastWriteTimeUtc(GetFileSystemInfo(path));
  302. }
  303. /// <summary>
  304. /// Gets the file stream.
  305. /// </summary>
  306. /// <param name="path">The path.</param>
  307. /// <param name="mode">The mode.</param>
  308. /// <param name="access">The access.</param>
  309. /// <param name="share">The share.</param>
  310. /// <param name="isAsync">if set to <c>true</c> [is asynchronous].</param>
  311. /// <returns>FileStream.</returns>
  312. public Stream GetFileStream(string path, FileOpenMode mode, FileAccessMode access, FileShareMode share, bool isAsync = false)
  313. {
  314. if (_supportsAsyncFileStreams && isAsync)
  315. {
  316. return new FileStream(path, GetFileMode(mode), GetFileAccess(access), GetFileShare(share), 262144, true);
  317. }
  318. return new FileStream(path, GetFileMode(mode), GetFileAccess(access), GetFileShare(share), 262144);
  319. }
  320. private FileMode GetFileMode(FileOpenMode mode)
  321. {
  322. switch (mode)
  323. {
  324. case FileOpenMode.Append:
  325. return FileMode.Append;
  326. case FileOpenMode.Create:
  327. return FileMode.Create;
  328. case FileOpenMode.CreateNew:
  329. return FileMode.CreateNew;
  330. case FileOpenMode.Open:
  331. return FileMode.Open;
  332. case FileOpenMode.OpenOrCreate:
  333. return FileMode.OpenOrCreate;
  334. case FileOpenMode.Truncate:
  335. return FileMode.Truncate;
  336. default:
  337. throw new Exception("Unrecognized FileOpenMode");
  338. }
  339. }
  340. private FileAccess GetFileAccess(FileAccessMode mode)
  341. {
  342. var val = (int)mode;
  343. return (FileAccess)val;
  344. }
  345. private FileShare GetFileShare(FileShareMode mode)
  346. {
  347. var val = (int)mode;
  348. return (FileShare)val;
  349. }
  350. public void SetHidden(string path, bool isHidden)
  351. {
  352. var info = GetFileInfo(path);
  353. if (info.Exists && info.IsHidden != isHidden)
  354. {
  355. if (isHidden)
  356. {
  357. File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Hidden);
  358. }
  359. else
  360. {
  361. FileAttributes attributes = File.GetAttributes(path);
  362. attributes = RemoveAttribute(attributes, FileAttributes.Hidden);
  363. File.SetAttributes(path, attributes);
  364. }
  365. }
  366. }
  367. public void SetReadOnly(string path, bool isReadOnly)
  368. {
  369. var info = GetFileInfo(path);
  370. if (info.Exists && info.IsReadOnly != isReadOnly)
  371. {
  372. if (isReadOnly)
  373. {
  374. File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.ReadOnly);
  375. }
  376. else
  377. {
  378. FileAttributes attributes = File.GetAttributes(path);
  379. attributes = RemoveAttribute(attributes, FileAttributes.ReadOnly);
  380. File.SetAttributes(path, attributes);
  381. }
  382. }
  383. }
  384. private static FileAttributes RemoveAttribute(FileAttributes attributes, FileAttributes attributesToRemove)
  385. {
  386. return attributes & ~attributesToRemove;
  387. }
  388. /// <summary>
  389. /// Swaps the files.
  390. /// </summary>
  391. /// <param name="file1">The file1.</param>
  392. /// <param name="file2">The file2.</param>
  393. public void SwapFiles(string file1, string file2)
  394. {
  395. if (string.IsNullOrEmpty(file1))
  396. {
  397. throw new ArgumentNullException("file1");
  398. }
  399. if (string.IsNullOrEmpty(file2))
  400. {
  401. throw new ArgumentNullException("file2");
  402. }
  403. var temp1 = Path.GetTempFileName();
  404. var temp2 = Path.GetTempFileName();
  405. // Copying over will fail against hidden files
  406. RemoveHiddenAttribute(file1);
  407. RemoveHiddenAttribute(file2);
  408. CopyFile(file1, temp1, true);
  409. CopyFile(file2, temp2, true);
  410. CopyFile(temp1, file2, true);
  411. CopyFile(temp2, file1, true);
  412. DeleteFile(temp1);
  413. DeleteFile(temp2);
  414. }
  415. /// <summary>
  416. /// Removes the hidden attribute.
  417. /// </summary>
  418. /// <param name="path">The path.</param>
  419. private void RemoveHiddenAttribute(string path)
  420. {
  421. if (string.IsNullOrEmpty(path))
  422. {
  423. throw new ArgumentNullException("path");
  424. }
  425. var currentFile = new FileInfo(path);
  426. // This will fail if the file is hidden
  427. if (currentFile.Exists)
  428. {
  429. if ((currentFile.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
  430. {
  431. currentFile.Attributes &= ~FileAttributes.Hidden;
  432. }
  433. }
  434. }
  435. public bool ContainsSubPath(string parentPath, string path)
  436. {
  437. if (string.IsNullOrEmpty(parentPath))
  438. {
  439. throw new ArgumentNullException("parentPath");
  440. }
  441. if (string.IsNullOrEmpty(path))
  442. {
  443. throw new ArgumentNullException("path");
  444. }
  445. return path.IndexOf(parentPath.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase) != -1;
  446. }
  447. public bool IsRootPath(string path)
  448. {
  449. if (string.IsNullOrEmpty(path))
  450. {
  451. throw new ArgumentNullException("path");
  452. }
  453. var parent = Path.GetDirectoryName(path);
  454. if (!string.IsNullOrEmpty(parent))
  455. {
  456. return false;
  457. }
  458. return true;
  459. }
  460. public string NormalizePath(string path)
  461. {
  462. if (string.IsNullOrEmpty(path))
  463. {
  464. throw new ArgumentNullException("path");
  465. }
  466. if (path.EndsWith(":\\", StringComparison.OrdinalIgnoreCase))
  467. {
  468. return path;
  469. }
  470. return path.TrimEnd(Path.DirectorySeparatorChar);
  471. }
  472. public string GetFileNameWithoutExtension(FileSystemMetadata info)
  473. {
  474. if (info.IsDirectory)
  475. {
  476. return info.Name;
  477. }
  478. return Path.GetFileNameWithoutExtension(info.FullName);
  479. }
  480. public string GetFileNameWithoutExtension(string path)
  481. {
  482. return Path.GetFileNameWithoutExtension(path);
  483. }
  484. public bool IsPathFile(string path)
  485. {
  486. if (string.IsNullOrWhiteSpace(path))
  487. {
  488. throw new ArgumentNullException("path");
  489. }
  490. // Cannot use Path.IsPathRooted because it returns false under mono when using windows-based paths, e.g. C:\\
  491. if (path.IndexOf("://", StringComparison.OrdinalIgnoreCase) != -1 &&
  492. !path.StartsWith("file://", StringComparison.OrdinalIgnoreCase))
  493. {
  494. return false;
  495. }
  496. return true;
  497. //return Path.IsPathRooted(path);
  498. }
  499. public void DeleteFile(string path)
  500. {
  501. var fileInfo = GetFileInfo(path);
  502. if (fileInfo.Exists)
  503. {
  504. if (fileInfo.IsHidden)
  505. {
  506. SetHidden(path, false);
  507. }
  508. if (fileInfo.IsReadOnly)
  509. {
  510. SetReadOnly(path, false);
  511. }
  512. }
  513. File.Delete(path);
  514. }
  515. public void DeleteDirectory(string path, bool recursive)
  516. {
  517. Directory.Delete(path, recursive);
  518. }
  519. public void CreateDirectory(string path)
  520. {
  521. Directory.CreateDirectory(path);
  522. }
  523. public List<FileSystemMetadata> GetDrives()
  524. {
  525. // Only include drives in the ready state or this method could end up being very slow, waiting for drives to timeout
  526. return DriveInfo.GetDrives().Where(d => d.IsReady).Select(d => new FileSystemMetadata
  527. {
  528. Name = GetName(d),
  529. FullName = d.RootDirectory.FullName,
  530. IsDirectory = true
  531. }).ToList();
  532. }
  533. private string GetName(DriveInfo drive)
  534. {
  535. return drive.Name;
  536. }
  537. public IEnumerable<FileSystemMetadata> GetDirectories(string path, bool recursive = false)
  538. {
  539. var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
  540. return ToMetadata(path, new DirectoryInfo(path).EnumerateDirectories("*", searchOption));
  541. }
  542. public IEnumerable<FileSystemMetadata> GetFiles(string path, bool recursive = false)
  543. {
  544. var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
  545. return ToMetadata(path, new DirectoryInfo(path).EnumerateFiles("*", searchOption));
  546. }
  547. public IEnumerable<FileSystemMetadata> GetFileSystemEntries(string path, bool recursive = false)
  548. {
  549. var directoryInfo = new DirectoryInfo(path);
  550. var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
  551. if (EnableFileSystemRequestConcat)
  552. {
  553. return ToMetadata(path, directoryInfo.EnumerateDirectories("*", searchOption))
  554. .Concat(ToMetadata(path, directoryInfo.EnumerateFiles("*", searchOption)));
  555. }
  556. return ToMetadata(path, directoryInfo.EnumerateFileSystemInfos("*", searchOption));
  557. }
  558. private IEnumerable<FileSystemMetadata> ToMetadata(string parentPath, IEnumerable<FileSystemInfo> infos)
  559. {
  560. return infos.Select(i =>
  561. {
  562. try
  563. {
  564. return GetFileSystemMetadata(i);
  565. }
  566. catch (PathTooLongException)
  567. {
  568. // Can't log using the FullName because it will throw the PathTooLongExceptiona again
  569. //Logger.Warn("Path too long: {0}", i.FullName);
  570. Logger.Warn("File or directory path too long. Parent folder: {0}", parentPath);
  571. return null;
  572. }
  573. }).Where(i => i != null);
  574. }
  575. public string[] ReadAllLines(string path)
  576. {
  577. return File.ReadAllLines(path);
  578. }
  579. public void WriteAllLines(string path, IEnumerable<string> lines)
  580. {
  581. File.WriteAllLines(path, lines);
  582. }
  583. public Stream OpenRead(string path)
  584. {
  585. return File.OpenRead(path);
  586. }
  587. public void CopyFile(string source, string target, bool overwrite)
  588. {
  589. File.Copy(source, target, overwrite);
  590. }
  591. public void MoveFile(string source, string target)
  592. {
  593. File.Move(source, target);
  594. }
  595. public void MoveDirectory(string source, string target)
  596. {
  597. Directory.Move(source, target);
  598. }
  599. public bool DirectoryExists(string path)
  600. {
  601. return Directory.Exists(path);
  602. }
  603. public bool FileExists(string path)
  604. {
  605. return File.Exists(path);
  606. }
  607. public string ReadAllText(string path)
  608. {
  609. return File.ReadAllText(path);
  610. }
  611. public byte[] ReadAllBytes(string path)
  612. {
  613. return File.ReadAllBytes(path);
  614. }
  615. public void WriteAllText(string path, string text, Encoding encoding)
  616. {
  617. File.WriteAllText(path, text, encoding);
  618. }
  619. public void WriteAllText(string path, string text)
  620. {
  621. File.WriteAllText(path, text);
  622. }
  623. public void WriteAllBytes(string path, byte[] bytes)
  624. {
  625. File.WriteAllBytes(path, bytes);
  626. }
  627. public string ReadAllText(string path, Encoding encoding)
  628. {
  629. return File.ReadAllText(path, encoding);
  630. }
  631. public IEnumerable<string> GetDirectoryPaths(string path, bool recursive = false)
  632. {
  633. var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
  634. return Directory.EnumerateDirectories(path, "*", searchOption);
  635. }
  636. public IEnumerable<string> GetFilePaths(string path, bool recursive = false)
  637. {
  638. var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
  639. return Directory.EnumerateFiles(path, "*", searchOption);
  640. }
  641. public IEnumerable<string> GetFileSystemEntryPaths(string path, bool recursive = false)
  642. {
  643. var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
  644. return Directory.EnumerateFileSystemEntries(path, "*", searchOption);
  645. }
  646. public virtual void SetExecutable(string path)
  647. {
  648. }
  649. }
  650. }