3

I am working on a project that receives a FileStream and it should receive it with a method that returns a MemoryStream.

How can I convert the MemoryStream to FileStream? I can't touch those codes and there lies the entire problem.

morcillo
  • 1,091
  • 5
  • 19
  • 51
  • Could you show an example of the signature of the method that you need to invoke and what you have as input. Your question is not clear why you need such conversion. – Darin Dimitrov Apr 16 '12 at 22:14
  • @DarinDimitrov. Your deleted answer looks valid. why did you delete it? – gdoron Apr 16 '12 at 22:16
  • 2
    Because I need to first understand what the OP is asking. His question doesn't make any sense and I prefer to have more information before posting a real answer. – Darin Dimitrov Apr 16 '12 at 22:19
  • I think the OP has an API like `void DoSomething(FileStream stream)` which he cannot change. He wants to use it with a `MemoryStream`, presumably without writing the data to a file first. – dtb Apr 16 '12 at 22:21
  • 5
    If this is the case then he cannot achieve that. A FileStream is a pointer to a file. You cannot possibly convert a MemoryStream that you have created in memory to a FileStream. This doesn't make any sense. He will have to flush the contents of this MemoryStream (that he presumably has) to a temporary file and then pass a FileStream to this file to his presumable API. – Darin Dimitrov Apr 16 '12 at 22:21
  • possible duplicate of [Convert a Stream to a FileStream in C#](http://stackoverflow.com/questions/3769067/convert-a-stream-to-a-filestream-in-c-sharp) – Austin Salonen Apr 16 '12 at 22:22
  • Any particular reason you're insisting on a `FileStream`? Would it not be possible to have the function that wants a `FileStream`, take a `Stream` instead? If all it wants is a stream of bytes, it shouldn't be caring where those bytes come from. And if it *needs* those bytes to come from a file, a `MemoryStream` simply won't cut it. – cHao Apr 18 '12 at 20:31

2 Answers2

3

If I understand your question correctly, you have a method that returns a MemoryStream, and you need to pass that stream to a method that takes a FileStream. I've seen several instances where colleagues of mine have written methods with a FileStream parameter when the parameter type could have been Stream. This is an excellent example of why it's better to use the less-derived type for the parameter.

The only solution I can think of is to write the memory stream to a temporary file: create a file stream for the temporary file, copy the memory stream to the file stream, and then either set the position to zero or close the stream and open a new stream on the same file, to pass to the method.

If I have misunderstood your question, please clarify.

phoog
  • 42,068
  • 6
  • 79
  • 117
0

This has been discussed at Convert a Stream to a FileStream in C#

Community
  • 1
  • 1
Beenish Khan
  • 1,571
  • 10
  • 18
  • 5
    The `using` statement would like to have a chat with you :) – Ed S. Apr 16 '12 at 22:16
  • Ed, this is to give an idea of how to do it, I trust them to take it on from here and close/flush in the best possible manner :) – Beenish Khan Apr 16 '12 at 22:17
  • 1
    This converts FileStream to MemoryStream, however question was how to convert MemoryStream to FileStream. – Ňuf Apr 16 '12 at 22:18
  • The Read method does not necessarily read *count* -many bytes into the buffer. It needs to be called repeatedly until the buffer is filled. – dtb Apr 16 '12 at 22:19
  • @BeenishKhan: Me thinks you are too trusting. It actually requires *fewer* characters to do the right thing, so I don't understand doing it the wrong way purposely (and yes, it is wrong because if `Read()` or `SetLength()` throws you don't clean up your stream. This is not equivalent to using a `using` statement, it contains a bug). – Ed S. Apr 16 '12 at 22:20
  • @Ed, to each his own. I like to point in the direction :) – Beenish Khan Apr 16 '12 at 22:22
  • @BeenishKhan: Problem being you pointed in the wrong direction. There is a difference between handing something a complete code sample ready for use in production code and using best practices / elminating obvious bugs. – Ed S. Apr 16 '12 at 22:23
  • @Ed, You can always make edit to answers if you believe there is a problem :) As XtremeProgramming says, just go ahead and fix it :) – Beenish Khan Apr 16 '12 at 22:24