views:

438

answers:

4

Hello, I wanted to ask if it is possible to redirect writes to a specific file, to a memory stream in my application so I can immediately read it. If it is possible please explain how to do so.

Thanks.

A: 

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.

Preet Sangha
I don't think this would be fast enough.
rbns
+3  A: 

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

Neil Barnwell
Yes, but be aware that a MemStram only has 1 Position - you cannot easily interleave read and write .
Henk Holterman
+1  A: 

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.

Vinay Sajip
Maybe I didn't understand what the echostream is for, but my scenario is that I have 2 application - one of them is an app I can't get control of, and it is writing to a log file, the other one is is my C# application. Now what I want is somehow redirect the writes to the log into a a stream in my C#. Also, the program writes ALOT of data to the log, and that's why I need it to be fast.
rbns
A: 

Help?

rbns