How to read a binary file using c#?

后端 未结 3 873
予麋鹿
予麋鹿 2021-01-03 12:43

I have got a binary file. I have no clue how to read this binary file using C#.

The definition of the records in the binary file as described in C++ is:



        
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-03 13:33

    See the sample below.

     public byte[] ReadByteArrayFromFile(string fileName)
    {
      byte[] buff = null;
      FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
      BinaryReader br = new BinaryReader(fs);
      long numBytes = new FileInfo(fileName).Length;
      buff = br.ReadBytes((int)numBytes);
      return buff;
    }
    

    Hope that helps...

提交回复
热议问题