基本的に、ストリームリーダーを使用して、ファイルからバイト配列にすべてのバイトを読み込んでいます。
私が宣言した配列は次のようになります。byte[] array = new byte[256];
配列 256 のサイズは、ファイルからバイト全体を読み取ることができますか? ファイルが 256 バイトではなく 500 バイトだと言っていますか?
または、配列の各要素のサイズは 256 バイトですか?
基本的に、ストリームリーダーを使用して、ファイルからバイト配列にすべてのバイトを読み込んでいます。
私が宣言した配列は次のようになります。byte[] array = new byte[256];
配列 256 のサイズは、ファイルからバイト全体を読み取ることができますか? ファイルが 256 バイトではなく 500 バイトだと言っていますか?
または、配列の各要素のサイズは 256 バイトですか?
使うだけ
byte[] byteData = System.IO.File.ReadAllBytes(fileName);
次に、プロパティを調べることで、ファイルの長さを確認できますbyteData.Length
。
代わりに使用できます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
)