C# 将 Stream 写入文件

时间:2021-03-11 05:42:09

public void StreamToFile(Stream stream,string fileName)

{

// 把 Stream 转换成 byte[]

byte[] bytes = new byte[stream.Length];

stream.Read(bytes, 0, bytes.Length);

// 设置当前流的位置为流的开始

stream.Seek(0, SeekOrigin.Begin);

// 把 byte[] 写入文件

FileStream fs = new FileStream(fileName, FileMode.Create);

BinaryWriter bw = new BinaryWriter(fs);

bw.Write(bytes);

bw.Close();

fs.Close();

}