Tôi tìm thấy mã sau trên web:
private byte [] StreamFile(string filename)
{
FileStream fs = new FileStream(filename, FileMode.Open,FileAccess.Read);
// Create a byte array of file stream length
byte[] ImageData = new byte[fs.Length];
//Read block of bytes from stream into the byte array
fs.Read(ImageData,0,System.Convert.ToInt32(fs.Length));
//Close the File Stream
fs.Close();
return ImageData; //return the byte data
}
Nó có đủ đáng tin cậy để sử dụng để chuyển đổi tệp thành byte [] trong c # không, hay có cách nào tốt hơn để thực hiện việc này?
fs.Close()vào phần cuối cùng của câu lệnh try-final bao gồm phần còn lại của mã, để đảm bảoClosenó thực sự được gọi.