diff --git a/Shared/Utils/DirectoryUtils.cs b/Shared/Utils/DirectoryUtils.cs
index 377d721..2f621e0 100644
--- a/Shared/Utils/DirectoryUtils.cs
+++ b/Shared/Utils/DirectoryUtils.cs
@@ -25,16 +25,15 @@ public static class DirectoryUtils {
///
/// 返回指定路径下的所有文件。
///
- public static IEnumerable EnumerateFiles(string path, bool topDirectoryOnly = false, string searchPattern = "*") {
+ public static IEnumerable GetFiles(string path, bool topDirectoryOnly = false, string searchPattern = "*") {
if (!Exists(path)) return [];
return Directory.EnumerateFiles(PathUtils.ToLongPath(path), searchPattern, topDirectoryOnly ? SearchOption.TopDirectoryOnly : SearchOption.AllDirectories);
}
///
- /// 返回指定路径下的所有文件夹。
- /// 路径不以分隔符结尾。
+ /// 返回指定路径下的所有文件夹,不以分隔符结尾。
///
- public static IEnumerable EnumerateDirectories(string path, bool topDirectoryOnly = false, string searchPattern = "*") {
+ public static IEnumerable GetDirectories(string path, bool topDirectoryOnly = false, string searchPattern = "*") {
if (!Exists(path)) return [];
return Directory.EnumerateDirectories(PathUtils.ToLongPath(path), searchPattern, topDirectoryOnly ? SearchOption.TopDirectoryOnly : SearchOption.AllDirectories);
}
diff --git a/Shared/Utils/FileUtils.cs b/Shared/Utils/FileUtils.cs
index 816a260..83794f3 100644
--- a/Shared/Utils/FileUtils.cs
+++ b/Shared/Utils/FileUtils.cs
@@ -21,6 +21,15 @@ public static class FileUtils {
File.WriteAllBytes(PathUtils.ToLongPath(filePath), content);
}
+ ///
+ /// 写入文件。
+ /// 如果文件或文件夹不存在,则会自动创建。若已存在,则会覆盖原文件。
+ ///
+ public static void Write(string filePath, IEnumerable content) {
+ DirectoryUtils.Create(filePath, isFilePath: true);
+ File.WriteAllBytes(PathUtils.ToLongPath(filePath), [..content]);
+ }
+
///
/// 将 写入文件。
/// 会将流的位置主动重置到开头。
@@ -28,13 +37,31 @@ public static class FileUtils {
///
public static void Write(string filePath, Stream stream) {
DirectoryUtils.Create(filePath, isFilePath: true);
- using FileStream fileStream = new(PathUtils.ToLongPath(filePath), FileMode.Create, FileAccess.Write);
+ using FileStream fileStream = OpenCreate(PathUtils.ToLongPath(filePath));
if (stream.CanSeek && stream.Position != 0) stream.Seek(0, SeekOrigin.Begin);
stream.CopyTo(fileStream);
}
#endregion
+ #region FileStream
+
+ ///
+ /// 打开该文件的只读 。
+ ///
+ public static FileStream OpenRead(string filePath) =>
+ new(PathUtils.ToLongPath(filePath), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
+
+ ///
+ /// 在指定路径创建文件,并打开 。
+ ///
+ public static FileStream OpenCreate(string filePath) {
+ DirectoryUtils.Create(filePath, isFilePath: true);
+ return new(PathUtils.ToLongPath(filePath), FileMode.Create);
+ }
+
+ #endregion
+
///
/// 删除文件。
///
@@ -54,16 +81,23 @@ public static class FileUtils {
new(PathUtils.ToLongPath(path));
///
- /// 打开该文件的只读 。
+ /// 复制文件。
+ /// 会创建对应文件夹、覆盖已有的文件。
///
- public static FileStream OpenRead(string filePath) =>
- new(PathUtils.ToLongPath(filePath), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
+ public static void Copy(string sourceFilePath, string destFilePath) {
+ if (sourceFilePath == destFilePath) return; // 如果复制同一个文件则跳过
+ DirectoryUtils.Create(destFilePath, isFilePath: true);
+ File.Copy(PathUtils.ToLongPath(sourceFilePath), PathUtils.ToLongPath(destFilePath), true);
+ }
///
- /// 在指定路径创建文件,并打开 。
+ /// 剪切文件。
+ /// 会创建对应文件夹、覆盖已有的文件。
///
- public static FileStream OpenCreate(string filePath) {
- DirectoryUtils.Create(filePath, isFilePath: true);
- return new(PathUtils.ToLongPath(filePath), FileMode.Create);
+ public static void Move(string sourceFilePath, string destFilePath) {
+ if (sourceFilePath == destFilePath) return; // 如果移动同一个文件则跳过
+ DirectoryUtils.Create(destFilePath, isFilePath: true);
+ File.Move(PathUtils.ToLongPath(sourceFilePath), PathUtils.ToLongPath(destFilePath));
}
+
}