Xabe.FileLock
Xabe.FileLock copied to clipboard
Xabe.FileLock
.NET Standard library providing exclusive lock on file.
Using
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.
Recommended using
ILock fileLock = new FileLock(file);
if(await fileLock.TryAcquire(TimeSpan.FromSeconds(15), true))
{
using(fileLock)
{
// file operations here
}
}
Lincence
Xabe.FileLock is licensed under MIT - see License for details.