/*------------------------
*Stream和byte[]之间的转换
*-----------------------*/
///<summary>
///将Stream转成byte[]
///</summary>
publicbyte[]StreamToBytes(Streamstream)
{
byte[]bytes=newbyte[stream.Length];
stream.Read(bytes,0,bytes.Length);
//设置当前流的位置为流的开始
stream.Seek(0,SeekOrigin.Begin);
returnbytes;
}
///<summary>
///将byte[]转成Stream
///</summary>
publicStreamBytesToStream(byte[]bytes)
{
Streamstream=newMemoryStream(bytes);
returnstream;
}
/*------------------------
*Stream和文件之间的转换
*-----------------------*/
///<summary>
///将Stream写入文件
///</summary>
publicvoidStreamToFile(Streamstream,stringfileName)
{
//把Stream转换成byte[]
byte[]bytes=newbyte[stream.Length];
stream.Read(bytes,0,bytes.Length);
//设置当前流的位置为流的开始
stream.Seek(0,SeekOrigin.Begin);
//把byte[]写入文件
FileStreamfs=newFileStream(fileName,FileMode.Create);
BinaryWriterbw=newBinaryWriter(fs);
bw.Write(bytes);
bw.Close();
fs.Close();
}
///<summary>
///从文件读取Stream
///</summary>
publicStreamFileToStream(stringfileName)
{
//打开文件
FileStreamfileStream=newFileStream(fileName,FileMode.Open,FileAccess.Read,FileShare.Read);
//读取文件的byte[]
byte[]bytes=newbyte[fileStream.Length];
fileStream.Read(bytes,0,bytes.Length);
fileStream.Close();
//把byte[]转换成Stream
Streamstream=newMemoryStream(bytes);
returnstream;
}