C# 将文件转换为字节数组 (byte[]) 的方法
下面是一个示例方法,用于将文件转换为 byte[] 数组:
using System.IO;
public byte[] ConvertFileToByteArray(string filePath)
{
byte[] fileData = null;
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
using (BinaryReader br = new BinaryReader(fs))
{
fileData = br.ReadBytes((int)fs.Length);
}
}
return fileData;
}
要使用此方法,只需将文件路径作为参数传递给 'ConvertFileToByteArray' 方法,它将返回一个 byte[] 数组。以下是使用示例:
string filePath = 'C:\path\to\file.txt';
byte[] fileBytes = ConvertFileToByteArray(filePath);
请注意,此方法使用了 'FileStream' 和 'BinaryReader' 类来读取文件的字节,并将其存储在 byte[] 数组中。
原文地址: https://www.cveoy.top/t/topic/qlEi 著作权归作者所有。请勿转载和采集!