Skip to content

Commit 4a66fcb

Browse files
committed
Improved file lock check on the Watcher using FileShare.None.
1 parent 36a6c94 commit 4a66fcb

1 file changed

Lines changed: 9 additions & 11 deletions

File tree

VidCoderCommon/Utilities/CommonFileUtilities.cs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,20 @@ public static bool IsDirectory(string filePath)
5757

5858
public static bool IsFileLocked(string filePath)
5959
{
60-
FileStream stream = null;
61-
6260
try
6361
{
64-
stream = File.OpenRead(filePath);
65-
}
66-
catch (Exception)
67-
{
68-
return true;
62+
// FileMode.Open: Open existing file
63+
// FileAccess.Read: Request read access
64+
// FileShare.None: Request exclusive access (no sharing)
65+
using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None))
66+
{
67+
return false; // Not locked
68+
}
6969
}
70-
finally
70+
catch
7171
{
72-
stream?.Close();
72+
return true; // Locked because it's being written to or opened elsewhere
7373
}
74-
75-
return false;
7674
}
7775

7876
private static void GetFilesOrVideoFoldersRecursive(DirectoryInfo directory, List<SourcePathWithType> paths, List<string> inacessibleDirectories, PickerFileFilter fileFilter)

0 commit comments

Comments
 (0)