1. 程式人生 > >C# 將文件轉換為 Stream

C# 將文件轉換為 Stream

c# close 讀取 filename files lose bytes 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