How do I detect when a directory or file changes without constant scanning

前端 未结 2 1408
南笙
南笙 2020-12-01 09:42

Other than reading all the files and comparing them with a previous snapshot, is there a way to detect when a directory changes in C# with Windows? I don\'t mind PInvoke if

2条回答
  •  没有蜡笔的小新
    2020-12-01 10:19

    I've had to do this for a program that would watch a directory and see if any new image files were added, and it would then automatically resize them. When someone would add multiple files at one time, the watcher wouldn't catch all the files since it was single threaded and was busy resizing one image while another was being dropped.

    I had to make this a multi-threaded app, where the main thread just watched the directory and added the files to a queue, and another thread would read from the queue and resize those images.

    That's something you might want to be careful of if you're going to be doing anything with the files.

提交回复
热议问题