开发者

How to monitoring directory for files in Delphi XE? [duplicate]

开发者 https://www.devze.com 2023-02-28 19:02 出处:网络
This question already has answers here: Closed 11 years ago. Poss开发者_开发技巧ible Duplicate: Delphi notification when a file gets updated
This question already has answers here: Closed 11 years ago.

Poss开发者_开发技巧ible Duplicate:

Delphi notification when a file gets updated

Need monitoring for create files and count them. OS: WinXP and high.


Last year I had the same need and tried out Iztok Kacin's Directory Watch. He responded to email and was very helpful in answering my questions.

His code worked, but I needed to be notified at the moment a file in a specific folder was closed, which for some odd reason, the ReadDirectoryChanges API (on which it depends) from Microsoft (maddeningly) doesn't provide. I also seem to recall that Iztok's code used threads and didn't feel light-weight enough for my needs.

I ended up using a surprisingly simple approach that has worked wonderfully for me. On a TTimer event that fires every few seconds, I use FindFirst on the folder I'm monitoring. All files found are put in a persistent TStringList. Any file that is found that isn't already in the StringList from previous TTimer events is new. (To detect if a file is closed, I try to open the file in exclusive mode. If I can't open it, then it's not added to the TStringList so it's checked on the next event.)

I was quite hesitant to use this approach, thinking it was far too brute-force. But, for the needs I had, this solution has worked out wonderfully and thankfully, involves a small amount of very simple code that is easy to understand and maintain.

HTH


You may want to take a look at this article (A Directory Monitor Class For Delphi), and also at this function from Windows API: ReadDirectoryChanges

You should also take a look at this SO question since it may suit your needs: Delphi notification when a file gets updated

0

精彩评论

暂无评论...
验证码 换一张
取 消