添加占用文件检测
This commit is contained in:
2 files changed
+49
No files matched your search
@@ -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>
|
||||
|
||||
Reference in new issue
Block a user