首页 > 代码库 > C# 将文件转换为 Stream

C# 将文件转换为 Stream

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;

}

C# 将文件转换为 Stream