I'm having a problem working with filesystemwatcher, it's driving me crazy.
Turns out I'm watching for new text files in a folder, when the Created Event is raised, I basically read it using the following code:
string txtTemp = File.ReadAllText(MyFilePath);
After that, I process the data in the txtTemp string, basically I read it's lines & store the data on a DB, pretty simple isn't it?
The problem is when an exception is raised in this process (let's say db connection failed), it doesn't matter if I catch it because for the next coming files the app will throw an exception saying
"The process cannot access the file 'theNewComingFile.txt' because it is being used by another process."
How come the new created file could be in use if it hasn't even been opened or read it? And the application keeps throwing this "process cannot access the file" exception for all new files.
The only thing we can do is to close and reopen the application, this resets the app and everything works fine again until an exception of any type is raised again> (gosh!)
Any ideas? any workaround? any thoughts? any suggestions any... anything? hehehehe =)
Thanks dudes !!
you can start your investigation using the Sysinternals Process Explorer. It will give you more details, particularly on which process holds the file.