Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to write a program to read *.txt files in a certain folder. The new *.txt files creat all the time. How can I write the code to read new files and keep a high running efficiency.
I need a solution. Your input will be appreciated!

What I have tried:

I have no idea. When the program run a time, all the file will be read a time. The efficiency is to low.
Posted
Updated 20-Dec-17 1:40am
Comments
A_Griffin 20-Dec-17 7:28am    
Use the FileSystemWatcher class to look for new files, and then read (only) those as they are created.

1 solution

As A_Griffin suggested, use a FileSystemWatcher[^] to monitor the folder.

Alternatively, keep a track of which files you have already processed, and only process ones which aren't on that list. (This method works better than a FileSystemWatcher if you app isn't running all the time). The list could be help in Text, JSON, XML, or even a DB - depends which is available and best for you.
 
Share this answer
 
Comments
peterxuv 20-Dec-17 7:57am    
Yes! The second solution should be the best one. But I hava a concerning while handreds of thousands of files in the folder, how can I make the app keeping a high efficiency when restart the app? If I track the name of files, should the app read all files again to check whether they are on the list? It's a waste of time.
OriginalGriff 20-Dec-17 8:17am    
It won't take long: Windows caches folder contents, so it can return them quickly - and Linq has the Except method which will remove all those already there.

If you don't read all names, you don't know what is there! (And there is no GetFiles method which only returns files since a particular time, so you will need to do this every time you want to check for new items anyway unless you use a combination of list and FileSystemWatcher)
peterxuv 20-Dec-17 8:35am    
OK, I will try it.
I wanted to read the file created time and record it in a text file. But PC date and time could be not right sometimes. So that it seems not strict.
@Griff, Thanks for your kindly help. ^_^
peterxuv 20-Dec-17 8:07am    
Thank you! @Griff

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900