This commit is contained in:
ShrlAlgo
2025-07-11 09:20:23 +08:00
parent c7b104f44f
commit 4d35cadb56
840 changed files with 102347 additions and 11595 deletions

View File

@@ -11,7 +11,35 @@ public static class IOAssist
var files = Directory.GetFiles($"{path}", "*", SearchOption.AllDirectories); //遍历所有文件夹
var list = files.Union(files).OrderBy(s => s);
}
/// <summary>
/// 获取当前文件的副本或备份文件名
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
/// <exception cref="FileNotFoundException"></exception>
public static string GetUniqueFilePath(string filePath)
{
if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath))
{
throw new FileNotFoundException($"文件'{filePath}'不存在。");
}
var dir = Path.GetDirectoryName(filePath);
var extension = Path.GetExtension(filePath);
var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(filePath);
var counter = 1;
while (true)
{
var candidateFileName = $"{fileNameWithoutExtension}.{counter:D3}{extension}";
var fullPath = Path.Combine(dir, candidateFileName);
if (!File.Exists(fullPath))
{
return fullPath;
}
counter++;
}
}
public static string GetRelativePath(string fromPath, string toPath)
{
Uri uri = new(fromPath);