简体   繁体   中英

Pass a MemoryStream object to a FileStream object

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){

}

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.

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM