Just a learner
Just a learner

Reputation: 28622

How to efficiently read the first 1000 bytes of a large file (larger than hundreds of GBs) in .net languages (C# or PowerShell etc)

How to efficiently read the first 1000 bytes of a large file (larger than hundreds of GBs) in .net languages (C# or PowerShell etc)? I have some binary files with private format and I need to read the first 1000 bytes to get some meta data of those files. Since they are pretty large, I want to know what's the best way to efficient get those information. Thanks.

Upvotes: 2

Views: 871

Answers (1)

Henk Holterman
Henk Holterman

Reputation: 273572

You can read the first section into a suitable binary array:

var stream = File.OpenRead(fileName);
byte[] binaryHeader = new byte[1000];
int actuallyRead = stream.Read(binaryHeader, 0, binaryHeader.Length);

How large the rest of the file is does not really matter.

Upvotes: 2

Related Questions