[英]How to convert CloudFileStream to FileStream?
我有一个使用IO.FileStream
读取文件的应用程序。 我们正在将存储移动到 Azure 文件存储。 如何在 c# CloudFileStream
IO.FileStream
转换为IO.FileStream
?
有 2 种可能的答案,取决于你想做什么,
如果您想编写一个可以处理两种类型流的类,那么最简单的方法是将该类基于所有流共有的抽象流类
如果要将数据从一个流复制到另一个流,则可以使用CopyTo 函数,但需要 .Net4 或更高版本
来自 MSDN
// Create the streams.
MemoryStream destination = new MemoryStream();
using (FileStream source = File.Open(@"c:\temp\data.dat",
FileMode.Open))
{
Console.WriteLine("Source length: {0}", source.Length.ToString());
// Copy source to destination.
source.CopyTo(destination);
}
Console.WriteLine("Destination length: {0}", destination.Length.ToString());
由于这些类是同级类(都派生自Stream
)并且没有父/子关系,因此无法直接将一个类转换为另一个类。 您需要使用本地文件来使用FileStream
而不是复制到 Azure 的CloudFileStream 。 https://msdn.microsoft.com/en-us/library/system.io.path.gettempfilename(v=vs.110).aspx )
笔记
Stream
作为大多数方法的参数,因为您将能够处理更多类型(包括这两种)。FileStream
并将CloudFileStream
内容复制到/从 lcal 文件复制 - 使用Path.GetTemplFileName ans name 创建临时文件,并确保在操作完成后清理它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.