添加占用文件检测

This commit is contained in:
HerobrineXia committed 2026-09-26 18:53:31 +08:00
1 parent 36ecaf29b6
commit 97d5dc9d17
2 files changed
+49

No files matched your search

+16
View File
@@ -461,6 +461,22 @@ public static class FileUtils {
public static bool Exists(string filePath)
=> File.Exists(PathUtils.ForApi(filePath));
/// <summary>
/// 检查文件是否未被其他进程占用
/// <para>被占用或无权读取时返回 <see langword="false"/>。</para>
/// </summary>
public static bool IsAvailable(string filePath) {
if (!Exists(filePath)) return true;
try {
Retrier.Attempt(maxAttempts: 3, delay: _ => TimeSpan.FromMilliseconds(200), isRetryAllowed: ex => ex is IOException, action: _ => {
using var stream = new FileStream(PathUtils.ForApi(filePath), FileMode.Open, FileAccess.Read, FileShare.None);
});
return true;
} catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) {
return false;
}
}
/// <summary>
/// 获取 <see cref="FileInfo"/> 对象。
/// </summary>