基本上我使用流读取器将所有字节从文件读取到字节数组中。声明字节数组以获取文件中的字节
我宣布的阵列看起来像这样:byte[] array = new byte[256];
阵列256的大小可以从文件中读取整个字节?说一个文件有500个字节而不是256个?
或者数组中的每个元素的大小为256字节?
基本上我使用流读取器将所有字节从文件读取到字节数组中。声明字节数组以获取文件中的字节
我宣布的阵列看起来像这样:byte[] array = new byte[256];
阵列256的大小可以从文件中读取整个字节?说一个文件有500个字节而不是256个?
或者数组中的每个元素的大小为256字节?
你可以使用File.ReadAllBytes
代替:
byte[] fileBytes = File.ReadAllBytes(path);
,或者如果你只是想知道的大小,用FileInfo
对象:
FileInfo f = new FileInfo(path);
long s1 = f.Length;
编辑:如果你想将它“在经典方式“如评论:
byte[] array;
using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
{
int num = 0;
long length = fileStream.Length;
if (length > 2147483647L)
{
throw new ArgumentException("File is greater than 2GB, hence it is too large!", "path");
}
int i = (int)length;
array = new byte[i];
while (i > 0)
{
int num2 = fileStream.Read(array, num, i);
num += num2;
i -= num2;
}
}
(反映通过ILSpy
)
是的我知道,但我想用古典的方式来做 –
@JoshuaBlack:虽然我更喜欢'File.ReadAllBytes',但增加了“古典方式”;) –
只需使用
byte[] byteData = System.IO.File.ReadAllBytes(fileName);
,然后你可以找到文件多久通过看byteData.Length
财产。
我真的不明白你的问题。你能改述一下吗? –
你用什么命令读入?您将无法将> 256个字节读入256字节数组。 – Joe
我正在使用BaseStream.Read函数。 –