说下有问题的程序,首先建立一个FileSystemWatcher,监控目录是否有新的文件到达,如果到达了就线程池分配一个线程来读取文件,然后进行后续处理,思路很简单,代码如下:
readonly FileSystemWatcher _watcher;
2:
string path)
4: {
new FileSystemWatcher
6: {
7: Path = path,
8: Filter = Constants.Configuration.ExcelFilter,
9: NotifyFilter = NotifyFilters.FileName |
10: NotifyFilters.LastWrite |
11: NotifyFilters.CreationTime
12: };
13:
14: _watcher.Created += OnNewFileComesin;
true;
16: }
17:
object sender, FileSystemEventArgs e)
19: {
20: Task.Factory.StartNew(() =>
21: {
return ReadFile(e.FullPath);
23: })
24: .ContinueWith(
//....
26: );
27: }
28:
string filePath)
30: {
new StreamReader(fullPath))
32: {
//...
return data;
35: }
36: }