ReadAsBytes
This commit is contained in:
1 parent
0e8671a7af
commit
c2d052e1ca
2 files changed
+64
-53
No files matched your search
@@ -20,13 +20,14 @@ public static class DirectoryUtils {
|
||||
=> Directory.Exists(PathUtils.WithLongPath(path));
|
||||
|
||||
/// <summary>
|
||||
/// 创建 <see cref="DirectoryInfo"/> 对象。
|
||||
/// 获取 <see cref="DirectoryInfo"/> 对象。
|
||||
/// </summary>
|
||||
public static DirectoryInfo GetInfo(string path)
|
||||
=> new(PathUtils.WithLongPath(path));
|
||||
|
||||
/// <summary>
|
||||
/// 返回指定路径下的所有文件,不以 \\?\ 开头。
|
||||
/// 如果文件夹不存在,返回空列表。
|
||||
/// </summary>
|
||||
public static IEnumerable<string> GetFiles(string path, bool topDirectoryOnly = false, string searchPattern = "*") {
|
||||
if (!DirectoryUtils.Exists(path)) return [];
|
||||
@@ -37,6 +38,7 @@ public static class DirectoryUtils {
|
||||
|
||||
/// <summary>
|
||||
/// 返回指定路径下的所有文件夹,不以分隔符结尾,不以 \\?\ 开头。
|
||||
/// 如果文件夹不存在,返回空列表。
|
||||
/// </summary>
|
||||
public static IEnumerable<string> GetDirectories(string path, bool topDirectoryOnly = false, string searchPattern = "*") {
|
||||
if (!DirectoryUtils.Exists(path)) return [];
|
||||
@@ -47,7 +49,7 @@ public static class DirectoryUtils {
|
||||
|
||||
/// <summary>
|
||||
/// 该文件夹是否为空。
|
||||
/// 如果文件夹不存在,也返回 true。
|
||||
/// 如果文件夹不存在,返回 true。
|
||||
/// </summary>
|
||||
public static bool IsEmpty(string path) {
|
||||
if (!DirectoryUtils.Exists(path)) return true;
|
||||
|
||||
+60
-51
@@ -1,9 +1,24 @@
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
using System.IO.Compression;
|
||||
|
||||
namespace MeloongCore;
|
||||
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 写入
|
||||
|
||||
/// <summary>
|
||||
@@ -44,6 +59,38 @@ public static class FileUtils {
|
||||
|
||||
#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 删除
|
||||
|
||||
/// <summary>
|
||||
@@ -128,26 +175,7 @@ public static class FileUtils {
|
||||
|
||||
#endregion
|
||||
|
||||
#region FileStream
|
||||
|
||||
/// <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 压缩包
|
||||
#region 压缩 / 解压
|
||||
|
||||
/// <summary>
|
||||
/// 以只读模式打开压缩文件。
|
||||
@@ -264,37 +292,18 @@ public static class FileUtils {
|
||||
=> File.Exists(PathUtils.WithLongPath(filePath));
|
||||
|
||||
/// <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>
|
||||
public static FileInfo GetInfo(string 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));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user