problems with FileSystemWatcher
-
hi everyone! i use FileSystemWatcher to watch a directory for incoming files. this works fine except if the file is written to the directory (e.g. copied by hand or the system) the very moment the program wants to access it via the FileSystemWatcher routines or events. so it is locked by the copy handler of windows. is there a chance to find out if the file is ready to be accessed (e.g. read), because otherwise a file.ioexception will be thrown. meaning, i would have to delay the access to this file, until it is ready for access. i had the idea of skipping the file until the next event takes place, but what if this causes the same problem, then this won't work. do you have any ideas or is there an event like "file accessible" or is there a chance to find out if no other handler "posesses" the file? thanks. stephan.
-
hi everyone! i use FileSystemWatcher to watch a directory for incoming files. this works fine except if the file is written to the directory (e.g. copied by hand or the system) the very moment the program wants to access it via the FileSystemWatcher routines or events. so it is locked by the copy handler of windows. is there a chance to find out if the file is ready to be accessed (e.g. read), because otherwise a file.ioexception will be thrown. meaning, i would have to delay the access to this file, until it is ready for access. i had the idea of skipping the file until the next event takes place, but what if this causes the same problem, then this won't work. do you have any ideas or is there an event like "file accessible" or is there a chance to find out if no other handler "posesses" the file? thanks. stephan.
This is a common problem. Did you search codeproject or google? These are some of the results: FileSystemWatcher - Created events fires too early[^] File in use[^]
#region signature my articles #endregion
-
This is a common problem. Did you search codeproject or google? These are some of the results: FileSystemWatcher - Created events fires too early[^] File in use[^]
#region signature my articles #endregion
so as far as i already read it is not too easy to just realize this within some minutes. it might be a trial and error until you find an acceptable solution for your own problem, right? i will try myself by tomorrow. but if someone already has a short solution "on the fly" please add it to my posting. i guess this might help everyone a lot. (some solutions might also be found in the links in the last posting by Giorgi Dalakishvili). thanks. stephan.
-
so as far as i already read it is not too easy to just realize this within some minutes. it might be a trial and error until you find an acceptable solution for your own problem, right? i will try myself by tomorrow. but if someone already has a short solution "on the fly" please add it to my posting. i guess this might help everyone a lot. (some solutions might also be found in the links in the last posting by Giorgi Dalakishvili). thanks. stephan.
i just found a simple solution but i am not very happy with it. i coded a function
private bool IsAccessible(string Path)
which checks, if the file is accessible or not. if it is the access trying to open the file is closed again, it returns true and the other operations continue. if it is not accessible, then the io-exception is caught and it returns false. when this function is called it is combined with a sleep(1000), meaning it tries to access the file, if it is accessible then it continues without waiting, if it is not, it waits for 1 sec. well you could reduce the amount of time for waiting to a fair value depending of the estimated time until the file should be ready. but after 1 sec most files are copied and ready for access :) -
hi everyone! i use FileSystemWatcher to watch a directory for incoming files. this works fine except if the file is written to the directory (e.g. copied by hand or the system) the very moment the program wants to access it via the FileSystemWatcher routines or events. so it is locked by the copy handler of windows. is there a chance to find out if the file is ready to be accessed (e.g. read), because otherwise a file.ioexception will be thrown. meaning, i would have to delay the access to this file, until it is ready for access. i had the idea of skipping the file until the next event takes place, but what if this causes the same problem, then this won't work. do you have any ideas or is there an event like "file accessible" or is there a chance to find out if no other handler "posesses" the file? thanks. stephan.
Hi Stephan, Before accessing a file for your processing, try opening a file with a WRITE access on it. If the file open fails with denied WRITE access, then another thread (Windows) might be using it. U can use this logic for any case (Files being copied, New file being created by another thread, etc.) Hope this helps...:) Regards, Krista