FileUtils.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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, bool toRecycleBin = false) {
  58. filePath = PathUtils.ToLongPath(filePath);
  59. if (toRecycleBin) {
  60. Microsoft.VisualBasic.FileIO.FileSystem.DeleteFile(filePath,
  61. Microsoft.VisualBasic.FileIO.UIOption.AllDialogs,
  62. Microsoft.VisualBasic.FileIO.RecycleOption.SendToRecycleBin,
  63. Microsoft.VisualBasic.FileIO.UICancelOption.ThrowException);
  64. } else {
  65. File.Delete(filePath);
  66. }
  67. }
  68. /// <summary>
  69. /// 确定指定的文件是否存在。
  70. /// </summary>
  71. public static bool Exists(string filePath) =>
  72. File.Exists(PathUtils.ToLongPath(filePath));
  73. /// <summary>
  74. /// 创建 <see cref="FileInfo"/> 对象。
  75. /// </summary>
  76. public static FileInfo GetInfo(string path) =>
  77. new(PathUtils.ToLongPath(path));
  78. /// <summary>
  79. /// 复制文件。
  80. /// 会创建对应文件夹、覆盖已有的文件。
  81. /// </summary>
  82. public static void Copy(string sourceFilePath, string destFilePath) {
  83. if (sourceFilePath == destFilePath) return; // 如果复制同一个文件则跳过
  84. DirectoryUtils.Create(destFilePath, isFilePath: true);
  85. File.Copy(PathUtils.ToLongPath(sourceFilePath), PathUtils.ToLongPath(destFilePath), true);
  86. }
  87. /// <summary>
  88. /// 剪切文件。
  89. /// 会创建对应文件夹、覆盖已有的文件。
  90. /// </summary>
  91. public static void Move(string sourceFilePath, string destFilePath) {
  92. if (sourceFilePath == destFilePath) return; // 如果移动同一个文件则跳过
  93. DirectoryUtils.Create(destFilePath, isFilePath: true);
  94. File.Move(PathUtils.ToLongPath(sourceFilePath), PathUtils.ToLongPath(destFilePath));
  95. }
  96. }