Read from a growing file in C#?

后端 未结 4 1568
粉色の甜心
粉色の甜心 2020-12-08 16:22

In C#/.NET (on Windows) is there a way to read a \"growing\" file using a file stream? The length of the file will be very small when the filestream is opened, but the file

4条回答
  •  星月不相逢
    2020-12-08 17:12

    The way i solved this is using the DirectoryWatcher / FilesystemWatcher class, and when it triggers on the file you want you open a FileStream and read it to the end. And when im done reading i save the position of the reader, so next time the DirectoryWatcher / FilesystemWatcher triggers i open a stream set the position to where i was last time.

    Calling FileStream.length is actualy very slow, i have had no performance issues with my solution ( I was im reading a "log" ranging from 10mb to 50 ish).

    To me the solution i describe is very simple and easy to maintain, i would try it and profile it. I dont think your going to get any performance issues based on it. I do this when ppl are playing a multi threaded game, taking their entire CPU and nobody has complained that my parser is more demanding then the competing parsers.

提交回复
热议问题