0

I have a MemoryStream object, and i want to write it to a file and pass it as a parameter in the following passFile method. The passFile method currently accepts a FileStream object as a parameter, but is there a way where i could convert the MemoryStream object to a FileStream object? Help

MemoryStream ms = new MemoryStream();

public void passFile (FileStream file){

}
user1315906
  • 3,374
  • 8
  • 30
  • 43

2 Answers2

2

No, you cannot cast it: MemoryStream and FileStream are siblings in the inheritance hierarchy, they cannot be cast to one another.

If the method cannot be re-written to take a Stream, you could write the content of the MemoryStream into a temporary file, and then open a FileStream based on the content of that file.

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

You could use a Stream abstraction, and pass any type of stream as FileStream, MemoryStream, etc... for sample:

public void passFile(Stream stream)
{
   // process stream
}
Felipe Oriani
  • 37,948
  • 19
  • 131
  • 194
  • Thanks for your reply. How do i then convert a Stream to a File ? i am looking at the link @dasblinkenlight has given, and still can't figure out what is the `File` object so i could use it in the method. – user1315906 Sep 18 '13 at 02:42
  • Look at the dasblinkenlight link: http://stackoverflow.com/questions/8624071/save-and-load-memorystream-to-from-a-file/8624188#8624188 ... there is a way to convert between them. – Felipe Oriani Sep 18 '13 at 11:22