I have a folder being watched by a service with a FileSystemWatcher
and I used this Answer to help me for large files being written to it.
However, I came to an issue if there are consecutive file creations about(100MB or more).
Q: How would can I tackle this issue? Like example around 10 files (100MB each) written to my folder.
Note: This folder is accessed through my network. There could be files created but not really complete/finished. the FileSystemWatcher
could process the file without it being written completely.
Current code for checking if opening file throws an exception
private static bool IsFileLocked(FileInfo file)
{
FileStream stream = null;
try
{
stream = file.Open(FileMode.Open,
FileAccess.Read, FileShare.None);
}
catch (IOException)
{
//the file is unavailable because it is:
//still being written to
//or being processed by another thread
//or does not exist (has already been processed)
return true;
}
finally
{
if (stream != null)
stream.Close();
}
//file is not locked
return false;
}
Current code checking if there is a file created.
private void FSWatcher_Created(object sender, System.IO.FileSystemEventArgs e)
{
var files = getFiles(FSWatcher.Path); //stores all filenames in the directory in a list
if (files.Count() > 0)
{
foreach (string file in files)
{
FileInfo file_info = new FileInfo(file);
while (IsFileLocked(file_info))
{
Thread.Sleep(5000); //Sleep if file unavailable
}
//code to process the file
}
//some code here
}
}