C# Stream 和 文件之间的转换

来源:互联网 发布:知乎电子书在电脑上看 编辑:程序博客网 时间:2024/06/01 15:36
/* - - - - - - - - - - - - - - - - - - - - - - - -   * Stream 和 文件之间的转换 转载请注明来自 http://www.shang11.com * - - - - - - - - - - - - - - - - - - - - - - - */ /// <summary> /// 将 Stream 写入文件 /// </summary> 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(); } 

0 0