read the content of binary files till the end of file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • abhii
    New Member
    • Feb 2008
    • 30

    read the content of binary files till the end of file

    hi every one,
    can u tel me please that how i can read the binary file till the end of file.......
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    What method are you using and do you receive an error?

    Comment

    • jjvainav
      New Member
      • Feb 2008
      • 25

      #3
      If you want to read all the bytes into an array, you can do this

      Code:
      string filePath = //some file path
      using(FileStream fs = new FileStream(filePath, FileMode.Open))
      {
          using(BinaryReader br = new BinaryReader(fs))
          {
              byte[] buffer = br.ReadBytes((int)br.BaseStream.Length);
          }
      }
      Hope this helps

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        A regular FileStream will work for the reading in of the bytes.
        BinaryReader is just used to implicitly create numerical values out of the data bytes.

        Comment

        • jjvainav
          New Member
          • Feb 2008
          • 25

          #5
          A regular FileStream will work for the reading in of the bytes.
          BinaryReader is just used to implicitly create numerical values out of the data bytes.
          He is correct, you don't need the BinaryReader. You can simply do:

          Code:
          using(FileStream fs = new FileStream(filePath, FileMode.Open))
          {
              byte[] buffer = new byte[fs.Length];
              fs.Read(buffer, 0, fs.Length);
          }

          Comment

          • balabaster
            Recognized Expert Contributor
            • Mar 2007
            • 798

            #6
            Originally posted by jjvainav
            He is correct, you don't need the BinaryReader. You can simply do:

            Code:
            using(FileStream fs = new FileStream(filePath, FileMode.Open))
            {
            byte[] buffer = new byte[fs.Length];
            fs.Read(buffer, 0, fs.Length);
            }
            To complicate things a little less could he not use:

            byte[] buffer = System.IO.File. ReadAllBytes("C :\MyFile.exe");

            ?

            Comment

            • Rajeshshewale
              New Member
              • Aug 2007
              • 8

              #7
              I have written file handling code here :
              just copy &paste;
              [Link Removed]

              Comment

              • MMcCarthy
                Recognized Expert MVP
                • Aug 2006
                • 14387

                #8
                @ Rajeshshewale

                If you wish to submit code please submit it in the thread and not using a link.

                ADMIN

                Comment

                Working...