C# 将文件转换为 Stream

时间:2024-03-10 10:57:54

public Stream FileToStream(string fileName)

{

// 打开文件

FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);

// 读取文件的 byte[]

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

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

fileStream.Close();

// 把 byte[] 转换成 Stream

Stream stream = new MemoryStream(bytes);

return stream;

}