Tuesday, 3 February 2009

C# Check if the file is ready for reading

This code checks if the file is ready for reading and that writing process is not having a lock on the file anymore.
private bool ReadyForReading(FileInfo file)
{
  try
  {
    using(new FileStream(file.FullName,FileMode.Open,FileAccess.Read,FileShare.None)){}                
    return true;
  }catch(Exception ex)
  {
    log.Warn(string.Format("Could not read file '{0}', probably writing in progress", file.Name),ex);
  return false;
  }
}

No comments:

Post a Comment