/// ------------------------------------------------------------------------------------ /// <summary> /// Gets the file info for the given filename (Using a case-insensitive lookup). /// </summary> /// <param name="filename">The fully-qualified file name</param> /// <param name="lockNeeded">Indicates what type of access is needed. If a /// permission is needed that is not available, throws an IOException</param> /// <returns>The internal file information if the file is found in the list of /// existing files</returns> /// <exception cref="ArgumentNullException"><c>filename</c> is a null reference</exception> /// <exception cref="ArgumentException"><c>filename</c> is an empty string ("") or /// contains only white space</exception> /// <exception cref="FileNotFoundException">File not found</exception> /// <exception cref="IOException">File locked</exception> /// ------------------------------------------------------------------------------------ private MockFile GetFileInfo(string filename, FileLockType lockNeeded) { if (filename == null) { throw new ArgumentNullException("filename"); } if (filename.Trim() == string.Empty) { throw new ArgumentException("Empty filename"); } MockFile finfo = m_existingFiles[GetKey(filename)]; if (lockNeeded == FileLockType.None) { return(finfo); } switch (finfo.Lock) { case FileLockType.Read: if (lockNeeded == FileLockType.Write) { throw new IOException("File " + filename + " is locked (open for read)."); } break; case FileLockType.Write: throw new IOException("File " + filename + " is locked (open for write)."); } return(finfo); }
public FileLock(string filePath, FileLockType lockType) { if(filePath == null) throw new ArgumentNullException("filePath"); if(!File.Exists(filePath)) try { File.Create(filePath).Close(); } catch { } CancellationTokenSource source = new CancellationTokenSource(); source.CancelAfter(20000); while(true) { try { if(lockType == FileLockType.Read) _Fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read); if(lockType == FileLockType.ReadWrite) _Fs = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.None); break; } catch(IOException) { Thread.Sleep(50); source.Token.ThrowIfCancellationRequested(); } } }
public FileLock(string filePath, FileLockType lockType) { if (filePath == null) { throw new ArgumentNullException(nameof(filePath)); } if (!File.Exists(filePath)) { try { File.Create(filePath).Dispose(); } catch { } } var source = new CancellationTokenSource(); source.CancelAfter(20000); while (true) { try { if (lockType == FileLockType.Read) { _fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read); } if (lockType == FileLockType.ReadWrite) { _fs = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.None); } break; } catch (IOException) { Thread.Sleep(50); source.Token.ThrowIfCancellationRequested(); } } }
public FileLock(string filePath, FileLockType lockType) { if (filePath == null) { throw new ArgumentNullException("filePath"); } if (!File.Exists(filePath)) { try { #if !NETCORE File.Create(filePath).Close(); #else File.Create(filePath).Flush(false); #endif } catch { } } CancellationTokenSource source = new CancellationTokenSource(); source.CancelAfter(20000); while (true) { try { if (lockType == FileLockType.Read) { _Fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read); } if (lockType == FileLockType.ReadWrite) { _Fs = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.None); } break; } catch (IOException) { Thread.Sleep(50); source.Token.ThrowIfCancellationRequested(); } } }
private FileLock CreateLock(FileLockType fileLockType) { return(new FileLock(Path.Combine(Folder.FullName, "StoreLock"), fileLockType)); }
/// ------------------------------------------------------------------------------------ /// <summary> /// Gets the file info for the given filename (Using a case-insensitive lookup). /// </summary> /// <param name="filename">The fully-qualified file name</param> /// <param name="lockNeeded">Indicates what type of access is needed. If a /// permission is needed that is not available, throws an IOException</param> /// <returns>The internal file information if the file is found in the list of /// existing files</returns> /// <exception cref="ArgumentNullException"><c>filename</c> is a null reference</exception> /// <exception cref="ArgumentException"><c>filename</c> is an empty string ("") or /// contains only white space</exception> /// <exception cref="FileNotFoundException">File not found</exception> /// <exception cref="IOException">File locked</exception> /// ------------------------------------------------------------------------------------ private MockFile GetFileInfo(string filename, FileLockType lockNeeded) { if (filename == null) throw new ArgumentNullException("filename"); if (filename.Trim() == string.Empty) throw new ArgumentException("Empty filename"); MockFile finfo = m_existingFiles[GetKey(filename)]; if (lockNeeded == FileLockType.None) return finfo; switch (finfo.Lock) { case FileLockType.Read: if (lockNeeded == FileLockType.Write) throw new IOException("File " + filename + " is locked (open for read)."); break; case FileLockType.Write: throw new IOException("File " + filename + " is locked (open for write)."); } return finfo; }