From c2d052e1cab871ef766b7e475c99a18099008b79 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=BE=99=E8=85=BE=E7=8C=AB=E8=B7=83?= <1043137532@qq.com>
Date: Thu, 7 May 2026 19:37:42 +0800
Subject: [PATCH] ReadAsBytes
---
Shared/Utils/DirectoryUtils.cs | 6 +-
Shared/Utils/FileUtils.cs | 111 ++++++++++++++++++---------------
2 files changed, 64 insertions(+), 53 deletions(-)
diff --git a/Shared/Utils/DirectoryUtils.cs b/Shared/Utils/DirectoryUtils.cs
index 7cc048f..a06dee7 100644
--- a/Shared/Utils/DirectoryUtils.cs
+++ b/Shared/Utils/DirectoryUtils.cs
@@ -20,13 +20,14 @@ public static class DirectoryUtils {
=> Directory.Exists(PathUtils.WithLongPath(path));
///
- /// 创建 对象。
+ /// 获取 对象。
///
public static DirectoryInfo GetInfo(string path)
=> new(PathUtils.WithLongPath(path));
///
/// 返回指定路径下的所有文件,不以 \\?\ 开头。
+ /// 如果文件夹不存在,返回空列表。
///
public static IEnumerable GetFiles(string path, bool topDirectoryOnly = false, string searchPattern = "*") {
if (!DirectoryUtils.Exists(path)) return [];
@@ -37,6 +38,7 @@ public static class DirectoryUtils {
///
/// 返回指定路径下的所有文件夹,不以分隔符结尾,不以 \\?\ 开头。
+ /// 如果文件夹不存在,返回空列表。
///
public static IEnumerable GetDirectories(string path, bool topDirectoryOnly = false, string searchPattern = "*") {
if (!DirectoryUtils.Exists(path)) return [];
@@ -47,7 +49,7 @@ public static class DirectoryUtils {
///
/// 该文件夹是否为空。
- /// 如果文件夹不存在,也返回 true。
+ /// 如果文件夹不存在,返回 true。
///
public static bool IsEmpty(string path) {
if (!DirectoryUtils.Exists(path)) return true;
diff --git a/Shared/Utils/FileUtils.cs b/Shared/Utils/FileUtils.cs
index 667af29..91bdc63 100644
--- a/Shared/Utils/FileUtils.cs
+++ b/Shared/Utils/FileUtils.cs
@@ -1,9 +1,24 @@
-using System.IO;
-using System.IO.Compression;
+using System.IO.Compression;
namespace MeloongCore;
public static class FileUtils {
+ #region 读取
+
+ ///
+ /// 读取文件中的所有字节。
+ ///
+ public static byte[] ReadAsBytes(string filePath)
+ => File.ReadAllBytes(PathUtils.WithLongPath(filePath));
+
+ ///
+ /// 打开该文件的只读 。
+ ///
+ public static FileStream ReadAsStream(string filePath)
+ => new(PathUtils.WithLongPath(filePath), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
+
+ #endregion
+
#region 写入
///
@@ -44,6 +59,38 @@ public static class FileUtils {
#endregion
+ #region 复制 / 剪切
+
+ ///
+ /// 复制文件。
+ /// 会创建对应文件夹、覆盖已有的文件。
+ /// 若原文件不存在,则不执行。
+ ///
+ public static void Copy(string sourceFilePath, string destFilePath) {
+ if (sourceFilePath == destFilePath) return; // 如果复制同一个文件则跳过
+ if (!FileUtils.Exists(sourceFilePath)) {
+ Logger.Info($"尝试复制文件,但原文件不存在,已跳过复制:{sourceFilePath} → {destFilePath}");
+ return;
+ }
+ DirectoryUtils.Create(destFilePath, isFilePath: true);
+ Logger.Trace($"复制文件:{sourceFilePath} → {destFilePath}");
+ File.Copy(PathUtils.WithLongPath(sourceFilePath), PathUtils.WithLongPath(destFilePath), true);
+ }
+
+ ///
+ /// 剪切文件。
+ /// 会创建对应文件夹、覆盖已有的文件。
+ ///
+ public static void Move(string sourceFilePath, string destFilePath) {
+ if (sourceFilePath == destFilePath) return; // 如果移动同一个文件则跳过
+ DirectoryUtils.Create(destFilePath, isFilePath: true);
+ FileUtils.Delete(destFilePath);
+ Logger.Trace($"剪切文件:{sourceFilePath} → {destFilePath}");
+ File.Move(PathUtils.WithLongPath(sourceFilePath), PathUtils.WithLongPath(destFilePath));
+ }
+
+ #endregion
+
#region 删除
///
@@ -128,26 +175,7 @@ public static class FileUtils {
#endregion
- #region FileStream
-
- ///
- /// 打开该文件的只读 。
- ///
- public static FileStream ReadAsStream(string filePath)
- => new(PathUtils.WithLongPath(filePath), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
-
- ///
- /// 在指定路径创建文件,并打开 。
- ///
- public static FileStream CreateAsStream(string filePath) {
- DirectoryUtils.Create(filePath, isFilePath: true);
- Logger.Trace($"创建文件流:{filePath}");
- return new(PathUtils.WithLongPath(filePath), FileMode.Create);
- }
-
- #endregion
-
- #region 压缩包
+ #region 压缩 / 解压
///
/// 以只读模式打开压缩文件。
@@ -264,37 +292,18 @@ public static class FileUtils {
=> File.Exists(PathUtils.WithLongPath(filePath));
///
- /// 创建 对象。
+ /// 在指定路径创建文件,并打开 。
+ ///
+ public static FileStream CreateAsStream(string filePath) {
+ DirectoryUtils.Create(filePath, isFilePath: true);
+ Logger.Trace($"创建文件流:{filePath}");
+ return new(PathUtils.WithLongPath(filePath), FileMode.Create);
+ }
+
+ ///
+ /// 获取 对象。
///
public static FileInfo GetInfo(string path)
=> new(PathUtils.WithLongPath(path));
- ///
- /// 复制文件。
- /// 会创建对应文件夹、覆盖已有的文件。
- /// 若原文件不存在,则不执行。
- ///
- public static void Copy(string sourceFilePath, string destFilePath) {
- if (sourceFilePath == destFilePath) return; // 如果复制同一个文件则跳过
- if (!FileUtils.Exists(sourceFilePath)) {
- Logger.Info($"尝试复制文件,但原文件不存在,已跳过复制:{sourceFilePath} → {destFilePath}");
- return;
- }
- DirectoryUtils.Create(destFilePath, isFilePath: true);
- Logger.Trace($"复制文件:{sourceFilePath} → {destFilePath}");
- File.Copy(PathUtils.WithLongPath(sourceFilePath), PathUtils.WithLongPath(destFilePath), true);
- }
-
- ///
- /// 剪切文件。
- /// 会创建对应文件夹、覆盖已有的文件。
- ///
- public static void Move(string sourceFilePath, string destFilePath) {
- if (sourceFilePath == destFilePath) return; // 如果移动同一个文件则跳过
- DirectoryUtils.Create(destFilePath, isFilePath: true);
- FileUtils.Delete(destFilePath);
- Logger.Trace($"剪切文件:{sourceFilePath} → {destFilePath}");
- File.Move(PathUtils.WithLongPath(sourceFilePath), PathUtils.WithLongPath(destFilePath));
- }
-
}