更新
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user