C# 声明用于从文件中获取字节的字节数组

C# 声明用于从文件中获取字节的字节数组,c#,C#,基本上,我使用流读取器将文件中的所有字节读入字节数组 我声明的数组如下所示:byte[]数组=新字节[256] 数组256的大小是否可以从文件中读取整个字节?说一个文件有500字节而不是256字节 或者数组中的每个元素的大小为256字节?请使用 byte[] byteData = System.IO.File.ReadAllBytes(fileName); 然后,您可以通过查看byteData.Length属性来了解文件的长度。您可以使用: 或者,如果您只想知道对象的大小: 编辑:如果您想按

基本上,我使用流读取器将文件中的所有字节读入字节数组

我声明的数组如下所示:
byte[]数组=新字节[256]

数组256的大小是否可以从文件中读取整个字节?说一个文件有500字节而不是256字节

或者数组中的每个元素的大小为256字节?

请使用

 byte[] byteData = System.IO.File.ReadAllBytes(fileName);
然后,您可以通过查看
byteData.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
反映)

我真的不明白你的问题。你能重新措辞吗?你用什么命令读入?您将无法在256字节数组中读入>256字节。我正在使用BaseStream.read函数。是的,我知道,但我想用经典的方式进行way@JoshuaBlack:添加了“经典方式”,尽管我更喜欢
File.ReadAllBytes
;)
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;
    }
}