FileUtils.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. namespace MeloongCore;
  2. public static class FileUtils {
  3. #region 写入
  4. /// <summary>
  5. /// 写入文件。
  6. /// 如果文件或文件夹不存在,则会自动创建。若已存在,则会覆盖原文件。
  7. /// </summary>
  8. public static void Write(string filePath, string text, Encoding? encoding = null) {
  9. DirectoryUtils.Create(filePath, isFilePath: true);
  10. File.WriteAllText(PathUtils.ToLongPath(filePath), text, encoding ?? new UTF8Encoding());
  11. }
  12. /// <summary>
  13. /// 写入文件。
  14. /// 如果文件或文件夹不存在,则会自动创建。若已存在,则会覆盖原文件。
  15. /// </summary>
  16. public static void Write(string filePath, byte[] content) {
  17. DirectoryUtils.Create(filePath, isFilePath: true);
  18. File.WriteAllBytes(PathUtils.ToLongPath(filePath), content);
  19. }
  20. /// <summary>
  21. /// 写入文件。
  22. /// 如果文件或文件夹不存在,则会自动创建。若已存在,则会覆盖原文件。
  23. /// </summary>
  24. public static void Write(string filePath, IEnumerable<byte> content) {
  25. DirectoryUtils.Create(filePath, isFilePath: true);
  26. File.WriteAllBytes(PathUtils.ToLongPath(filePath), [..content]);
  27. }
  28. /// <summary>
  29. /// 将 <paramref name="stream" /> 写入文件。
  30. /// 会将流的位置主动重置到开头。
  31. /// 如果文件或文件夹不存在,则会自动创建。若已存在,则会覆盖原文件。
  32. /// </summary>
  33. public static void Write(string filePath, Stream stream) {
  34. DirectoryUtils.Create(filePath, isFilePath: true);
  35. using FileStream fileStream = OpenCreate(PathUtils.ToLongPath(filePath));
  36. if (stream.CanSeek && stream.Position != 0) stream.Seek(0, SeekOrigin.Begin);
  37. stream.CopyTo(fileStream);
  38. }
  39. #endregion
  40. #region FileStream
  41. /// <summary>
  42. /// 打开该文件的只读 <see cref="FileStream"/>。
  43. /// </summary>
  44. public static FileStream OpenRead(string filePath) =>
  45. new(PathUtils.ToLongPath(filePath), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  46. /// <summary>
  47. /// 在指定路径创建文件,并打开 <see cref="FileStream"/>。
  48. /// </summary>
  49. public static FileStream OpenCreate(string filePath) {
  50. DirectoryUtils.Create(filePath, isFilePath: true);
  51. return new(PathUtils.ToLongPath(filePath), FileMode.Create);
  52. }
  53. #endregion
  54. /// <summary>
  55. /// 删除文件。
  56. /// </summary>
  57. public static void Delete(string filePath) =>
  58. File.Delete(PathUtils.ToLongPath(filePath));
  59. /// <summary>
  60. /// 确定指定的文件是否存在。
  61. /// </summary>
  62. public static bool Exists(string filePath) =>
  63. File.Exists(PathUtils.ToLongPath(filePath));
  64. /// <summary>
  65. /// 创建 <see cref="FileInfo"/> 对象。
  66. /// </summary>
  67. public static FileInfo GetInfo(string path) =>
  68. new(PathUtils.ToLongPath(path));
  69. /// <summary>
  70. /// 复制文件。
  71. /// 会创建对应文件夹、覆盖已有的文件。
  72. /// </summary>
  73. public static void Copy(string sourceFilePath, string destFilePath) {
  74. if (sourceFilePath == destFilePath) return; // 如果复制同一个文件则跳过
  75. DirectoryUtils.Create(destFilePath, isFilePath: true);
  76. File.Copy(PathUtils.ToLongPath(sourceFilePath), PathUtils.ToLongPath(destFilePath), true);
  77. }
  78. /// <summary>
  79. /// 剪切文件。
  80. /// 会创建对应文件夹、覆盖已有的文件。
  81. /// </summary>
  82. public static void Move(string sourceFilePath, string destFilePath) {
  83. if (sourceFilePath == destFilePath) return; // 如果移动同一个文件则跳过
  84. DirectoryUtils.Create(destFilePath, isFilePath: true);
  85. File.Move(PathUtils.ToLongPath(sourceFilePath), PathUtils.ToLongPath(destFilePath));
  86. }
  87. }