diff --git a/Pilz.IO/FileLocker.cs b/Pilz.IO/FileLocker.cs new file mode 100644 index 0000000..298d4f5 --- /dev/null +++ b/Pilz.IO/FileLocker.cs @@ -0,0 +1,117 @@ +using System; +using System.IO; + +namespace Pilz.IO +{ + public partial class FileLocker : IDisposable + { + private FileStream fsLock = null; + + /// + /// Defines the file path to the file that should be locked. + /// + public string FilePath { get; private set; } + /// + /// Defines the file path to the lock file that is used to identify the file lock. + /// + public string LockFile { get; private set; } + /// + /// Defines if the file is locked privatly by this instance. + /// + public bool LockedPrivate { get; private set; } = false; + + /// + /// Defines if the file is locked by an other instance/program/user. + /// + public bool LockedExternal + { + get + { + if (LockedPrivate) + return false; + else + { + string lockFile = FilePath + ".lock"; + bool isLocked = false; + + if (File.Exists(lockFile)) + { + try + { + var fs = new FileStream(lockFile, FileMode.Open, FileAccess.Read); + fs.Close(); + } + catch (IOException ex) + { + isLocked = true; + } + } + + return isLocked; + } + } + } + + /// + /// Generate a new instance of . + /// + /// The file path to the file that should be locked. + public FileLocker(string filePath) : this(filePath, true) + { + } + + /// + /// Generate a new instance of + /// + /// The file path to the file that should be locked. + /// Defines if the file should be locked automatically right after creating this instance. + public FileLocker(string filePath, bool autoLock) + { + FilePath = filePath; + LockFile = filePath + ".lock"; + if (autoLock) Lock(); + } + + /// + /// Locks the file, if not already locked privatly. + /// + public void Lock() + { + if (!LockedPrivate) + { + fsLock = new FileStream(LockFile, FileMode.Create, FileAccess.ReadWrite); + LockedPrivate = true; + } + } + + /// + /// Unlocks the file, if locked privatly. + /// + public void Unlock() + { + if (LockedPrivate) + { + fsLock.Close(); + fsLock.Dispose(); + fsLock = null; + File.Delete(LockFile); + LockedPrivate = false; + } + } + + #region IDisposable + private bool disposedValue; + + protected virtual void Dispose(bool disposing) + { + if (!disposedValue) + Unlock(); + } + + public void Dispose() + { + Dispose(true); + } + #endregion + } +} \ No newline at end of file diff --git a/Pilz.IO/Pilz.IO.csproj b/Pilz.IO/Pilz.IO.csproj index 519a863..0ffdc17 100644 --- a/Pilz.IO/Pilz.IO.csproj +++ b/Pilz.IO/Pilz.IO.csproj @@ -95,6 +95,7 @@ +