Redirect writes to a file to a stream C#

会有一股神秘感。 提交于 2019-12-01 00:58:43

Make sure your application works with Stream, and then you can use the MemoryStream concrete implementation to accomplish streaming without a file.

What you need is a stream which writes to a file as well as to another stream which you are watching - a stream splitter, commonly known in the Unix world as tee.

For .NET you can use this implementation of a stream splitter. Pass it a FileStream (so that the file gets written to) and for the second stream, pass a Stream implementation which does whatever you want when the stream is written to (for example, a subclass of MemoryStream with an overridden Write method).

Update: I see from your comment that my answer isn't what you need - I didn't realise from your original question that you had no control over the app writing to file.

In order to get this kind of control, for finest control you will need to go to a low-level driver which intercepts system calls - much like FileMon does. For slightly less control, you can use System.IO.FileSystemWatcher.

I don't know how to do this in the way you say but you could use a FileWatcher to hook file. If its written to you'll be notified and you can read it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!