[英]How to convert CloudFileStream to FileStream?
I have an application which uses IO.FileStream
to read files.我有一个使用
IO.FileStream
读取文件的应用程序。 We are moving storage to Azure File storage.我们正在将存储移动到 Azure 文件存储。 How do I convert
CloudFileStream
to IO.FileStream
in c#?如何在 c#
CloudFileStream
IO.FileStream
转换为IO.FileStream
?
there are 2 possible answers depending on what you want to do,有 2 种可能的答案,取决于你想做什么,
if you want to write a class that can work with both types of stream then the simplest way is to base that class around the abstract stream class which is common to all streams如果您想编写一个可以处理两种类型流的类,那么最简单的方法是将该类基于所有流共有的抽象流类
if you want to copy data from one stream to another then you can use the CopyTo function however requires .Net4 or higher如果要将数据从一个流复制到另一个流,则可以使用CopyTo 函数,但需要 .Net4 或更高版本
from MSDN来自 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());
Since these classes are siblings (both derive from Stream
) and do not have parent/child relationship there is no direct way to convert one to another.由于这些类是同级类(都派生自
Stream
)并且没有父/子关系,因此无法直接将一个类转换为另一个类。 You need to use local file to use FileStream
and than copy to Azure's CloudFileStream .您需要使用本地文件来使用
FileStream
而不是复制到 Azure 的CloudFileStream 。 https://msdn.microsoft.com/en-us/library/system.io.path.gettempfilename(v=vs.110).aspx ) https://msdn.microsoft.com/en-us/library/system.io.path.gettempfilename(v=vs.110).aspx )
Notes笔记
Stream
as parameter to most methods as you'll be able to handle more types (including these two).Stream
作为大多数方法的参数,因为您将能够处理更多类型(包括这两种)。FileStream
and copy content of CloudFileStream
to/from lcal file before calling your methods - create temporary files using Path.GetTemplFileName ans name and make sure to clean them up after operation is complete.FileStream
并将CloudFileStream
内容复制到/从 lcal 文件复制 - 使用Path.GetTemplFileName ans name 创建临时文件,并确保在操作完成后清理它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.