ManagedFileSystem.cs 25 KB

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