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);
}
}
}
}
Loading

VulpesRank #55 · 29.2K reputationPosted Feb 10, 2014, 6:16 AM EST
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
string filePath = null;
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
filePath = ap.FullName;
}
Console.WriteLine("Total Bytes = {0} bytes", Totbyte);//the code works great up to this point
string temPath = Path.GetTempFileName();
byte[] data = null;
if (File.Exists(temPath))
{
data = File.ReadAllBytes(filePath);
File.WriteAllBytes(temPath, data);
}
}
}
}
However, it's much easier to just use File.Copy.
tom jones18 reputationPosted Feb 10, 2014, 6:22 AM EST
VulpesRank #55 · 29.2K reputationPosted Feb 10, 2014, 6:28 AM EST
tom jones18 reputationPosted Feb 10, 2014, 6:39 AM EST
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[] data = File.ReadAllBytes(temPath);
File.WriteAllBytes(temPath2, data);
}
}
}
}
VulpesRank #55 · 29.2K reputationPosted Feb 10, 2014, 7:39 AM EST
Note though that 'data' is only accessible within the 'if' statement. If you want it to be accessible throughout the Main method, then you need to declare it outside the ''if' statement and initialize it to null, say, as I did in my first post.
However, another problem with your code is that both temporary files are zero bytes long. Copying from one to the other therefore has no effect.
tom jones18 reputationPosted Feb 10, 2014, 7:49 AM EST
VulpesRank #55 · 29.2K reputationPosted Feb 10, 2014, 9:17 AM EST
byte[] bytes = File.ReadAllBytes(filePath);
byte[] bits = new byte[8 * bytes.Length];
for(int i = 0; i < bytes.Length; i++)
{
byte[] temp = GetBits(bytes[i]);
for(int j = 0; j < 8; j++)
{
bits[8 * i + j] = temp[j];
}
}
// helper method
static byte[] GetBits(byte b)
{
byte[] bits = new byte[8];
string temp = Convert.ToString(b, 2).PadLeft(8, '0');
for(int i = 0; i < 8; i++) bits[i] = (byte)(temp[i] - 48);
return bits;
}