ReadAllBytes Method of the File Class PROBLEM
-
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, theReadAllBytes
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
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.
-
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.
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
-
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
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?
-
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); } } }
}
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
-
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
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?
-
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?
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
-
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?
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 -
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?
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!!
-
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 KreskowiakI really should have read all of the comments before posting my (redundant) suggestion of BitArray.... :doh:
-
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, theReadAllBytes
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
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