Need To Create Simple C# Windows App - Looks In Fo

2019-09-22 15:04发布

问题:

This question already has an answer here:

  • Processing Folder With Multiple Files Using FileSystemWatcher and C# 3 answers

Could someone please help me create a simple windows C# app that would look in a specified folder? The app basically will on the click of a button watch a specified folder and if a new file is put into the folder it opens the file and does some processing with it. I also need to consider if multiple files are created into the folder at relatively the same time.

I can create the project and the screen with Visual Studio, I just really need some help with creating the code to loop, watch and process files in the folder. I've tried using FileSystemWatcher but the problem is it will not process multiple files (or at least files that were put into the folder at the relative same time). Also it will not process a second file if it's added to the folder while the first file is being processed.

Any help or guidance on this would be really REALLY helpful. Please feel free to explain in great detail. :D

回答1:

According to this question: FileSystemWatcher vs polling to watch for file changes it is mostly a problem of the InternalBufferSize try to push that value a little up. But there are also some situations, where even that won't work e.g. network shares.

The alternative would be polling as said in the other question, which means that you need a list of the current files in the folder and a timer, that cyclic compares the items inside the folder with which are in the stored list.