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
VulpesPosted Feb 10, 2014, 2:17 PM
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;
}
tom jonesPosted Feb 10, 2014, 12:49 PM
VulpesPosted Feb 10, 2014, 12:39 PM
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 jonesPosted Feb 10, 2014, 11:39 AM
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);
}
}
}
}
VulpesPosted Feb 10, 2014, 11:28 AM
tom jonesPosted Feb 10, 2014, 11:22 AM
VulpesPosted Feb 10, 2014, 11:16 AM
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.