I have a WPF app that pulls Word files from a DB as Byte(), saves to a temp directory then interop processes the file to make another with x pages - that is also saved to the same directory. Finally everything is wrapped in an email and mailed - when the process ends all the files are deleted and the process starts again.
This works perfectly for a while, then 'jams' with 'the file is open with another process'
Everything is run from an async sub that hands off parts of the constructs to different functions.
I'm trying to find a way to see if the file is still open (and looping until it is closed) before deleting.
Dim vPath As String = AppContext.BaseDirectory
If Not System.IO.Directory.Exists(vPath & "Temp_Files") Then
System.IO.Directory.CreateDirectory(vPath & "Temp_Files")
End If
For Each deleteFile In Directory.GetFiles(vPath & "Temp_Files", "*.*",
SearchOption.TopDirectoryOnly)
File.Delete(deleteFile)
Next
One assumes if these are files "open for write", that their properties will change once closed. You can monitor for changes in the "states" of files using a FileSystemWatcher.
https://learn.microsoft.com/en-us/dotnet/api/system.io.filesystemwatcher?view=net-8.0