1

The application I'm attempting to create would read the binary code of any file and create a file with the exact same binary code, creating a copy.

While writing a program that reads a file and writes it somewhere else, I was running into encoding issues, so I hypothesize that reading as straight binary will overcome this.

The file being read into the application is important, as after I get this to work I will add additional functionality to search within or manipulate the file's data as it is read.

Update: I'd like to thank everyone that took the time to answer, I now have a working solution. Wolfwyrd's answer was exactly what I needed.

Justin Stryker
  • 373
  • 4
  • 13

5 Answers5

3

BinaryReader will handle reading the file into a byte buffer. BinaryWriter will handle dumping those bytes back out to another file. Your code will be something like:

using (var binReader = new System.IO.BinaryReader(System.IO.File.OpenRead("PATHIN")))
using (var binWriter = new System.IO.BinaryWriter(System.IO.File.OpenWrite("PATHOUT")))
{
    byte[] buffer = new byte[512];
    while (binReader.Read(buffer, 0, 512) != 0)
    {
        binWriter.Write(buffer);
    }
}

Here we cycle a buffer of 512 bytes and immediately write it out to the other file. You would need to choose sensible sizes for your own buffer (there's nothing stopping you reading the entire file if it's reasonably sized). As you mentioned doing pattern matching you will need to consider the case where a pattern overlaps a buffered read if you do not load the whole file into a single byte array.

This SO Question has more details on best practices on reading large files.

Community
  • 1
  • 1
Wolfwyrd
  • 15,716
  • 5
  • 47
  • 67
1

Have a look at using BinaryReader Class

Reads primitive data types as binary values in a specific encoding.

and maybe BinaryReader.ReadBytes Method

Reads the specified number of bytes from the current stream into a byte array and advances the current position by that number of bytes.

also BinaryWriter Class

Writes primitive types in binary to a stream and supports writing strings in a specific encoding.

Another good example C# - Copying Binary Files

Adriaan Stander
  • 162,879
  • 31
  • 289
  • 284
1

for instance, one char at a time.

using (BinaryReader writer = new BinaryWrite(File.OpenWrite("target"))
{
    using (BinaryReader reader = new BinaryReader(File.OpenRead("source"))
    {
        var nextChar = reader.Read();
        while (nextChar != -1)
        {
            writer.Write(Convert.ToChar(nextChar));
            nextChar = reader.Read();
        }
    }
}
Jodrell
  • 34,946
  • 5
  • 87
  • 124
0

The application I'm attempting to create would read the binary code of any file and create a file with the exact same binary code, creating a copy.

Is this for academic purposes? Or do you actually just want to copy a file?

If the latter, you'll want to just use the System.IO.File.Copy method.

Timothy Shields
  • 75,459
  • 18
  • 120
  • 173