AsyncFile.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System.IO;
  2. namespace MediaBrowser.Model.IO
  3. {
  4. /// <summary>
  5. /// Helper class to create async <see cref="FileStream" />s.
  6. /// </summary>
  7. public static class AsyncFile
  8. {
  9. /// <summary>
  10. /// Gets the default <see cref="FileStreamOptions"/> for reading files async.
  11. /// </summary>
  12. public static FileStreamOptions ReadOptions => new FileStreamOptions()
  13. {
  14. Options = FileOptions.Asynchronous
  15. };
  16. /// <summary>
  17. /// Gets the default <see cref="FileStreamOptions"/> for writing files async.
  18. /// </summary>
  19. public static FileStreamOptions WriteOptions => new FileStreamOptions()
  20. {
  21. Mode = FileMode.OpenOrCreate,
  22. Access = FileAccess.Write,
  23. Share = FileShare.None,
  24. Options = FileOptions.Asynchronous
  25. };
  26. /// <summary>
  27. /// Creates, or truncates and overwrites, a file in the specified path.
  28. /// </summary>
  29. /// <param name="path">The path and name of the file to create.</param>
  30. /// <returns>A <see cref="FileStream" /> that provides read/write access to the file specified in path.</returns>
  31. public static FileStream Create(string path)
  32. => new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.None, IODefaults.FileStreamBufferSize, FileOptions.Asynchronous);
  33. /// <summary>
  34. /// Opens an existing file for reading.
  35. /// </summary>
  36. /// <param name="path">The file to be opened for reading.</param>
  37. /// <returns>A read-only <see cref="FileStream" /> on the specified path.</returns>
  38. public static FileStream OpenRead(string path)
  39. => new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, IODefaults.FileStreamBufferSize, FileOptions.Asynchronous);
  40. /// <summary>
  41. /// Opens an existing file for writing.
  42. /// </summary>
  43. /// <param name="path">The file to be opened for writing.</param>
  44. /// <returns>An unshared <see cref="FileStream" /> object on the specified path with Write access.</returns>
  45. public static FileStream OpenWrite(string path)
  46. => new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None, IODefaults.FileStreamBufferSize, FileOptions.Asynchronous);
  47. }
  48. }