ReadAsBytes

This commit is contained in:
龙腾猫跃 committed 2026-05-07 19:37:42 +08:00
1 parent 0e8671a7af
commit c2d052e1ca
2 files changed
+64 -53

No files matched your search

+4 -2
View File
@@ -20,13 +20,14 @@ public static class DirectoryUtils {
=> Directory.Exists(PathUtils.WithLongPath(path)); => Directory.Exists(PathUtils.WithLongPath(path));
/// <summary> /// <summary>
/// 创建 <see cref="DirectoryInfo"/> 对象。 /// 获取 <see cref="DirectoryInfo"/> 对象。
/// </summary> /// </summary>
public static DirectoryInfo GetInfo(string path) public static DirectoryInfo GetInfo(string path)
=> new(PathUtils.WithLongPath(path)); => new(PathUtils.WithLongPath(path));
/// <summary> /// <summary>
/// 返回指定路径下的所有文件,不以 \\?\ 开头。 /// 返回指定路径下的所有文件,不以 \\?\ 开头。
/// 如果文件夹不存在,返回空列表。
/// </summary> /// </summary>
public static IEnumerable<string> GetFiles(string path, bool topDirectoryOnly = false, string searchPattern = "*") { public static IEnumerable<string> GetFiles(string path, bool topDirectoryOnly = false, string searchPattern = "*") {
if (!DirectoryUtils.Exists(path)) return []; if (!DirectoryUtils.Exists(path)) return [];
@@ -37,6 +38,7 @@ public static class DirectoryUtils {
/// <summary> /// <summary>
/// 返回指定路径下的所有文件夹,不以分隔符结尾,不以 \\?\ 开头。 /// 返回指定路径下的所有文件夹,不以分隔符结尾,不以 \\?\ 开头。
/// 如果文件夹不存在,返回空列表。
/// </summary> /// </summary>
public static IEnumerable<string> GetDirectories(string path, bool topDirectoryOnly = false, string searchPattern = "*") { public static IEnumerable<string> GetDirectories(string path, bool topDirectoryOnly = false, string searchPattern = "*") {
if (!DirectoryUtils.Exists(path)) return []; if (!DirectoryUtils.Exists(path)) return [];
@@ -47,7 +49,7 @@ public static class DirectoryUtils {
/// <summary> /// <summary>
/// 该文件夹是否为空。 /// 该文件夹是否为空。
/// 如果文件夹不存在,也返回 true。 /// 如果文件夹不存在,返回 true。
/// </summary> /// </summary>
public static bool IsEmpty(string path) { public static bool IsEmpty(string path) {
if (!DirectoryUtils.Exists(path)) return true; if (!DirectoryUtils.Exists(path)) return true;
+60 -51
View File
@@ -1,9 +1,24 @@
using System.IO; using System.IO.Compression;
using System.IO.Compression;
namespace MeloongCore; namespace MeloongCore;
public static class FileUtils { public static class FileUtils {
#region 读取
/// <summary>
/// 读取文件中的所有字节。
/// </summary>
public static byte[] ReadAsBytes(string filePath)
=> File.ReadAllBytes(PathUtils.WithLongPath(filePath));
/// <summary>
/// 打开该文件的只读 <see cref="FileStream"/>。
/// </summary>
public static FileStream ReadAsStream(string filePath)
=> new(PathUtils.WithLongPath(filePath), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
#endregion
#region 写入 #region 写入
/// <summary> /// <summary>
@@ -44,6 +59,38 @@ public static class FileUtils {
#endregion #endregion
#region 复制 / 剪切
/// <summary>
/// 复制文件。
/// 会创建对应文件夹、覆盖已有的文件。
/// 若原文件不存在,则不执行。
/// </summary>
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);
}
/// <summary>
/// 剪切文件。
/// 会创建对应文件夹、覆盖已有的文件。
/// </summary>
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 删除 #region 删除
/// <summary> /// <summary>
@@ -128,26 +175,7 @@ public static class FileUtils {
#endregion #endregion
#region FileStream #region 压缩 / 解压
/// <summary>
/// 打开该文件的只读 <see cref="FileStream"/>。
/// </summary>
public static FileStream ReadAsStream(string filePath)
=> new(PathUtils.WithLongPath(filePath), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
/// <summary>
/// 在指定路径创建文件,并打开 <see cref="FileStream"/>。
/// </summary>
public static FileStream CreateAsStream(string filePath) {
DirectoryUtils.Create(filePath, isFilePath: true);
Logger.Trace($"创建文件流:{filePath}");
return new(PathUtils.WithLongPath(filePath), FileMode.Create);
}
#endregion
#region 压缩包
/// <summary> /// <summary>
/// 以只读模式打开压缩文件。 /// 以只读模式打开压缩文件。
@@ -264,37 +292,18 @@ public static class FileUtils {
=> File.Exists(PathUtils.WithLongPath(filePath)); => File.Exists(PathUtils.WithLongPath(filePath));
/// <summary> /// <summary>
/// 创建 <see cref="FileInfo"/> 对象。 /// 在指定路径创建文件,并打开 <see cref="FileStream"/>。
/// </summary>
public static FileStream CreateAsStream(string filePath) {
DirectoryUtils.Create(filePath, isFilePath: true);
Logger.Trace($"创建文件流:{filePath}");
return new(PathUtils.WithLongPath(filePath), FileMode.Create);
}
/// <summary>
/// 获取 <see cref="FileInfo"/> 对象。
/// </summary> /// </summary>
public static FileInfo GetInfo(string path) public static FileInfo GetInfo(string path)
=> new(PathUtils.WithLongPath(path)); => new(PathUtils.WithLongPath(path));
/// <summary>
/// 复制文件。
/// 会创建对应文件夹、覆盖已有的文件。
/// 若原文件不存在,则不执行。
/// </summary>
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);
}
/// <summary>
/// 剪切文件。
/// 会创建对应文件夹、覆盖已有的文件。
/// </summary>
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));
}
} }