.NET Standard library providing exclusive lock on file.
Install the Xabe.FileLock NuGet package via nuget:
PM> Install-Package Xabe.FileLock
Creating file lock:
ILock fileLock = new FileLock(file);
fileLock.TryAcquire(TimeSpan.FromSeconds(15), true);
This will create lock file with extension ".lock" in the same directory. Example: "/tmp/data.txt" -> "/tmp/data.lock".
Last parameter is optional and defines if lock should be automatically refreshing before expired.
If file already has lock file, and it time haven't expired, method returns false.
ILock fileLock = new FileLock(file);
if(await fileLock.TryAcquire(TimeSpan.FromSeconds(15), true))
{
using(fileLock)
{
// file operations here
}
}
Xabe.FileLock is licensed under MIT - see License for details.