[英]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. 我有一个
MemoryStream
对象,我想将其写入文件并在以下passFile
方法passFile
其作为参数传递。 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? passFile
方法当前接受FileStream
对象作为参数,但是有没有一种方法可以将MemoryStream
对象转换为FileStream
对象? 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. 不,您不能强制转换:
MemoryStream
和FileStream
是继承层次结构中的兄弟姐妹,它们不能互相强制转换。
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. 如果无法重写该方法以获取
Stream
,则可以将MemoryStream
的内容写入一个临时文件中 ,然后根据该文件的内容打开一个FileStream
。
You could use a Stream
abstraction, and pass any type of stream as FileStream
, MemoryStream
, etc... for sample: 您可以使用
Stream
抽象,并将任何类型的流作为FileStream
, MemoryStream
等传递给示例:
public void passFile(Stream stream)
{
// process stream
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.