Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. ReadAllBytes Method of the File Class PROBLEM

ReadAllBytes Method of the File Class PROBLEM

Scheduled Pinned Locked Moved C#
csharplinqdata-structureshelp
12 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Richard DeemingR Richard Deeming

    computerpublic wrote:

    //the code works great up to this point

    Well, it runs, but it might not be doing what you think it's doing. You're displaying the length of the last file in the directory, but your loop and variable name suggest you want to display the total length of all files in the directory. That's quite a simple change:

    foreach (FileInfo ap in Arr)
    Totbyte += ap.Length;

    Note the "+=" instead of just "=".

    computerpublic wrote:

    //want to set up an array for the exact file length above and read all bytes into the array

    Firstly, what would the length of the files in C:\Folder have to do with the length of a temporary file? Secondly, the ReadAllBytes method will return an array of the correct size to contain all of the bytes in the file you're reading. You don't need to specify the size of the array.

    byte[] data = File.ReadAllBytes(tempPath);

    Thirdly, since you're just copying one file to another, why not simple use the File.Copy method?

    if (File.Exists(temPath))
    {
    File.Copy(temPath, temPath2, true);
    }

    Finally, look at the documentation of GetTempFileName:

    http://msdn.microsoft.com/en-us/library/system.io.path.gettempfilename%28v=vs.110%29.aspx[^]

    Creates a uniquely named, zero-byte temporary file on disk and returns the full path of that file.

    That means that temPath will always exist, and will be an empty file. Copying it over another empty file will have no effect. Perhaps if you explain what you're trying to do, we might be able to point you in the right direction. :)


    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

    C Offline
    C Offline
    computerpublic
    wrote on last edited by
    #3

    I already have the file on my computer. I do not want to read the file to my computer. I want to read the file into an array. I did not know that the array would automatically be the right size. I do not know which of the identifiers is the array. How do I go about accessing the array.

    Richard DeemingR 1 Reply Last reply
    0
    • C computerpublic

      I already have the file on my computer. I do not want to read the file to my computer. I want to read the file into an array. I did not know that the array would automatically be the right size. I do not know which of the identifiers is the array. How do I go about accessing the array.

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #4

      It's still not clear what you're trying to achieve. If you want to read the content of a binary file:

      // Read the content of the specified file into an array called "data":
      byte[] data = File.ReadAllBytes("C:\Folder\TheFileToRead.ext");

      for (int index = 0; index < data.Length; index++)
      {
      // Access the array:
      byte theByteAtThisIndex = data[index];

      ...
      

      }


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      C 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        It's still not clear what you're trying to achieve. If you want to read the content of a binary file:

        // Read the content of the specified file into an array called "data":
        byte[] data = File.ReadAllBytes("C:\Folder\TheFileToRead.ext");

        for (int index = 0; index < data.Length; index++)
        {
        // Access the array:
        byte theByteAtThisIndex = data[index];

        ...
        

        }


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        C Offline
        C Offline
        computerpublic
        wrote on last edited by
        #5

        Your explanation is great. I know I am using a byte array (I don't think a bit array exist), but how do i go about accessing the bits of each byte in the array?

        Richard DeemingR M 2 Replies Last reply
        0
        • C computerpublic

          using System;
          using System.IO;
          using System.Collections.Generic;
          using System.Linq;
          using System.Text;

          namespace Applica
          {
          class Program
          {
          static void Main(string[] args)
          {
          long Totbyte = 0;//file length initialize to 0
          DirectoryInfo da = new DirectoryInfo("C:\\Folder");//local file on the computer
          FileInfo[] Arr = da.GetFiles();
          foreach (FileInfo ap in Arr)
          Totbyte = ap.Length;//real file length on local computer
          Console.WriteLine("Total Bytes = {0} bytes", Totbyte);//the code works great up to this point

                  //want to set up an array for the exact file length above and read all bytes into the array
                  //not sure of what I am doing below, but its not working out
                  string temPath = Path.GetTempFileName();
                  string temPath2 = Path.GetTempFileName();
                      if (File.Exists(temPath))
                      {
                          byte\[Totbyte\] data = File.ReadAllBytes(temPath);
                          File.WriteAllBytes(temPath2, data);
                      }
              }
          }
          

          }

          B Offline
          B Offline
          BillWoodruff
          wrote on last edited by
          #6

          I can't understand what you are trying to do here: 1. you get a Directory, and then you iterate over all its files setting a variable to the current file length: which means the value of 'Totbyte is always going to be equal to the length of the last file read. Did you mean to make a total of the length of all files ? in which case you should have: Totbyte += ap.Length; ? 2. then you create two temporary files, and check if one of them exists, and read its bytes into a byte[], which makes no sense, since the temp file will have no content. Isn't it the case that the file (files ?) you want to read have something to do with the first part of your code, where you create an array of files from a Directory path ?

          “But I don't want to go among mad people,” Alice remarked. “Oh, you can't help that,” said the Cat: “we're all mad here. I'm mad. You're mad.” “How do you know I'm mad?” said Alice. “You must be," said the Cat, or you wouldn't have come here.” Lewis Carroll

          C 1 Reply Last reply
          0
          • B BillWoodruff

            I can't understand what you are trying to do here: 1. you get a Directory, and then you iterate over all its files setting a variable to the current file length: which means the value of 'Totbyte is always going to be equal to the length of the last file read. Did you mean to make a total of the length of all files ? in which case you should have: Totbyte += ap.Length; ? 2. then you create two temporary files, and check if one of them exists, and read its bytes into a byte[], which makes no sense, since the temp file will have no content. Isn't it the case that the file (files ?) you want to read have something to do with the first part of your code, where you create an array of files from a Directory path ?

            “But I don't want to go among mad people,” Alice remarked. “Oh, you can't help that,” said the Cat: “we're all mad here. I'm mad. You're mad.” “How do you know I'm mad?” said Alice. “You must be," said the Cat, or you wouldn't have come here.” Lewis Carroll

            C Offline
            C Offline
            computerpublic
            wrote on last edited by
            #7

            Ideally what I am trying to do is copy of a file off my desktop in an Array of Bits (not Bytes). Can you please explain how this can be done?

            D 1 Reply Last reply
            0
            • C computerpublic

              Your explanation is great. I know I am using a byte array (I don't think a bit array exist), but how do i go about accessing the bits of each byte in the array?

              Richard DeemingR Offline
              Richard DeemingR Offline
              Richard Deeming
              wrote on last edited by
              #8

              You would use bitwise operators to extract the individual bits from each byte: Understand how bitwise operators work (C# and VB.NET examples)[^]


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

              1 Reply Last reply
              0
              • C computerpublic

                Ideally what I am trying to do is copy of a file off my desktop in an Array of Bits (not Bytes). Can you please explain how this can be done?

                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #9

                Richard Deeming already gave you the answer to this. You could use the BitArray class[^] to give you access to the bits in each byte, but it's not exactly what I would call "efficient". You would still have to read the file into a byte array and then pass the byte array to the BitArray constructor.

                A guide to posting questions on CodeProject[^]
                Dave Kreskowiak

                M 1 Reply Last reply
                0
                • C computerpublic

                  Your explanation is great. I know I am using a byte array (I don't think a bit array exist), but how do i go about accessing the bits of each byte in the array?

                  M Offline
                  M Offline
                  Matt T Heffron
                  wrote on last edited by
                  #10

                  You can use the BitArray class to treat the array of bytes as an array of bits.

                  byte[] data = File.ReadAllBytes("C:\Folder\TheFileToRead.ext");
                  BitArray dataAsBits = new BitArray(data);

                  See BitArray Class[^] Note the order of the bits in the bytes!!

                  1 Reply Last reply
                  0
                  • D Dave Kreskowiak

                    Richard Deeming already gave you the answer to this. You could use the BitArray class[^] to give you access to the bits in each byte, but it's not exactly what I would call "efficient". You would still have to read the file into a byte array and then pass the byte array to the BitArray constructor.

                    A guide to posting questions on CodeProject[^]
                    Dave Kreskowiak

                    M Offline
                    M Offline
                    Matt T Heffron
                    wrote on last edited by
                    #11

                    I really should have read all of the comments before posting my (redundant) suggestion of BitArray.... :doh:

                    1 Reply Last reply
                    0
                    • Richard DeemingR Richard Deeming

                      computerpublic wrote:

                      //the code works great up to this point

                      Well, it runs, but it might not be doing what you think it's doing. You're displaying the length of the last file in the directory, but your loop and variable name suggest you want to display the total length of all files in the directory. That's quite a simple change:

                      foreach (FileInfo ap in Arr)
                      Totbyte += ap.Length;

                      Note the "+=" instead of just "=".

                      computerpublic wrote:

                      //want to set up an array for the exact file length above and read all bytes into the array

                      Firstly, what would the length of the files in C:\Folder have to do with the length of a temporary file? Secondly, the ReadAllBytes method will return an array of the correct size to contain all of the bytes in the file you're reading. You don't need to specify the size of the array.

                      byte[] data = File.ReadAllBytes(tempPath);

                      Thirdly, since you're just copying one file to another, why not simple use the File.Copy method?

                      if (File.Exists(temPath))
                      {
                      File.Copy(temPath, temPath2, true);
                      }

                      Finally, look at the documentation of GetTempFileName:

                      http://msdn.microsoft.com/en-us/library/system.io.path.gettempfilename%28v=vs.110%29.aspx[^]

                      Creates a uniquely named, zero-byte temporary file on disk and returns the full path of that file.

                      That means that temPath will always exist, and will be an empty file. Copying it over another empty file will have no effect. Perhaps if you explain what you're trying to do, we might be able to point you in the right direction. :)


                      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                      B Offline
                      B Offline
                      BillWoodruff
                      wrote on last edited by
                      #12

                      Upvoted ! At the time I wrote my response to this thread, Richard's reply was not available here, which is becoming all too typical: there is often a gap of hours before content on QA and Forums shows up, and, yet, other parts of CodeProject seem to be updated instantly. Had I seen Richard's response, I would not have posted.

                      “But I don't want to go among mad people,” Alice remarked. “Oh, you can't help that,” said the Cat: “we're all mad here. I'm mad. You're mad.” “How do you know I'm mad?” said Alice. “You must be," said the Cat, or you wouldn't have come here.” Lewis Carroll

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups