I have a task to process files from a folder using C#, the naming convention of the files is like this:
0000001.log
0000002.log
....
00nnnnn.log
Given a parameter x, all the files with the name smaller than x will be processed. After processing, the file will be deleted.
At the same time, another application will constantly adding log files to this folder, with an incremental number.
I know I can use GetFiles
or EnumerateFiles
& linq in C# to filter the files I need.
If I understand correctly, these two method will load all the file names into memory first, and then filter out the ones I need.
Since nnnn
is quite a large number, I think it will be more efficient if I have a way to retrieve only the file names I need, ie x number of files, without loading all file names
Is this possible?
You can use file system watcher to receive a notification on a new file creation:
System.IO.FileSystemWatcher m_Watcher = new System.IO.FileSystemWatcher();
m_Watcher.Path = folderPath;
m_Watcher.Filter = strFilter;
m_Watcher.NotifyFilter = NotifyFilters.LastAccess |
NotifyFilters.LastWrite |
NotifyFilters.FileName |
NotifyFilters.DirectoryName;
m_Watcher.Changed += new FileSystemEventHandler(OnChanged);
m_Watcher.Created += new FileSystemEventHandler(OnChanged);
While strFilter is the filter for the file names for example: "*.txt";
For further information https://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher(v=vs.110).aspx
您可以进行for迭代并在其中添加if条件。如果条件为true,则将其放到该文件中。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.