Exemplo n.º 1
0
        /// <summary>
        /// Determines if a file is locked using <see cref="T:ByteDev.Io.Locking.FileLocker" />.
        /// </summary>
        /// <param name="fileInfo">File to check.</param>
        /// <returns>True if the file is locked; otherwise false.</returns>
        /// <exception cref="T:System.ArgumentNullException"><paramref name="fileInfo" /> is null.</exception>
        /// <exception cref="T:System.IO.FileNotFoundException">File does not exist.</exception>
        public static bool IsLocked(FileInfo fileInfo)
        {
            if (fileInfo == null)
            {
                throw new ArgumentNullException(nameof(fileInfo));
            }

            ThrowIfNotExists(fileInfo);

            var fileLockInfo = new FileLockInfo(fileInfo);

            return(fileLockInfo.LockFile.Exists);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Unlocks a file. This method will delete a corresponding .lock file if it exists.
        /// </summary>
        /// <param name="fileInfo">File to unlock.</param>
        /// <exception cref="T:System.ArgumentNullException"><paramref name="fileInfo" /> is null.</exception>
        /// <exception cref="T:System.IO.FileNotFoundException">File does not exist.</exception>
        public static void Unlock(FileInfo fileInfo)
        {
            if (fileInfo == null)
            {
                throw new ArgumentNullException(nameof(fileInfo));
            }

            ThrowIfNotExists(fileInfo);

            var fileLockInfo = new FileLockInfo(fileInfo);

            fileLockInfo.LockFile.DeleteIfExists();
        }
Exemplo n.º 3
0
        /// <summary>
        /// Locks a file. This method will create a corresponding .lock file if it does not exist.
        /// </summary>
        /// <param name="fileInfo">File to lock.</param>
        /// <returns>File lock info.</returns>
        /// <exception cref="T:System.ArgumentNullException"><paramref name="fileInfo" /> is null.</exception>
        /// <exception cref="T:System.IO.FileNotFoundException">File does not exist.</exception>
        /// <exception cref="T:System.InvalidOperationException">Lock file cannot be created as it already exists.</exception>
        public static FileLockInfo Lock(FileInfo fileInfo)
        {
            if (fileInfo == null)
            {
                throw new ArgumentNullException(nameof(fileInfo));
            }

            ThrowIfNotExists(fileInfo);

            var fileLockInfo = new FileLockInfo(fileInfo);

            if (fileLockInfo.LockFile.Exists)
            {
                throw new InvalidOperationException($"Lock file: '{fileLockInfo.LockFile.FullName}' cannot be created as it already exists.");
            }

            CreateLockFile(fileLockInfo.LockFile.FullName);

            return(fileLockInfo);
        }