How do I check for a file being open in a folder?

  • Thread starter Thread starter Cam Evenson
  • Start date Start date
C

Cam Evenson

Guest
I have an application which analyses CSV files in a folder and exports the data they contain to a database. The CSV files are written to the folder by a lab testing systems. There could be a time when I attempt to open the CSV while the Lab system is writing to it. How do I take capture the error that the file can not be opened and trigger my application to re try later on. The application currently has a thread timer which looks at the folder.

The collection of the file paths in the CheckFiles Sub is where I am getting the error. It is called from within the automatic function which checks for new files in a timed process.

Shared Sub CheckFiles()
_allFiles = Directory.GetDirectories(PathToWatch, TypeToWatch, SearchOpt).ToList

For i As Integer = Files.Count - 1 To 0 Step -1
If Not _allFiles.Contains(Files(i)) Then
Files.RemoveAt(i)
End If
Next
For i As Integer = _allFiles.Count - 1 To 0 Step -1

If Not Files.Contains(_allFiles(i)) AndAlso CheckIfExist(_allFiles(i)) Then

NewFiles.Add(_allFiles(i))
Files.Add(_allFiles(i))

End If
Next
End Sub

Continue reading...
 
Back
Top