简体   繁体   中英

Read and write of binary file in c#

I have read a binary file in using C# as per the following code. Then I tried to write this binary data in another binary file. But I found that when I opened these 2 files in Winmerge, there is a difference in both binary files. ie read file and written file. Could you please suggest why there is a difference if I just read files and rewrite?

       string fileNameWithPath_ = "1.pwpmi";
       string newfileNameWithPath_ = "2.pwpmi";

        System.IO.FileStream fileStream = new System.IO.FileStream(fileNameWithPath_, System.IO.FileMode.Open,
            System.IO.FileAccess.Read);
        System.IO.BinaryReader binReader = new System.IO.BinaryReader(fileStream, Encoding.ASCII);


        char[] chararr = new char[fileStream.Length];

        chararr = binReader.ReadChars((int)fileStream.Length);
        byte[] buffer = binReader.ReadBytes((int)fileStream.Length);

        byte[] bytes = new byte[fileStream.Length];
        fileStream.Read(bytes,0, (int)fileStream.Length);

        byte[] fileBytes = System.IO.File.ReadAllBytes(fileNameWithPath_);
        string stringbyte1 = Encoding.ASCII.GetString(fileBytes);

        binReader.Close();
        fileStream.Close();
        System.IO.BinaryWriter binWriter =
        new System.IO.BinaryWriter(System.IO.File.Open(newfileNameWithPath_, System.IO.FileMode.Create));
        binWriter.Flush();
        binWriter.Write(stringbyte1);
        binWriter.Close();

It appears you have tried a few different methods and have actually come quite close to the working one. The issue is likely in the way your read your binary data as one data type and write it back into output as another. Try sticking to bytes :

    string fileNameWithPath_ = "1.pwpmi";
    string newfileNameWithPath_ = "2.pwpmi";

    System.IO.FileStream fileStream = new System.IO.FileStream(fileNameWithPath_, System.IO.FileMode.Open,
        System.IO.FileAccess.Read);
    System.IO.BinaryReader binReader = new System.IO.BinaryReader(fileStream, Encoding.ASCII);
    byte[] fileBytes = binReader.ReadBytes((int)fileStream.Length);
    //byte[] fileBytes = System.IO.File.ReadAllBytes(fileNameWithPath_); // this also works

    binReader.Close();
    fileStream.Close();
    System.IO.BinaryWriter binWriter =
    new System.IO.BinaryWriter(System.IO.File.Open(newfileNameWithPath_, System.IO.FileMode.Create));
    binWriter.Flush();
    binWriter.Write(fileBytes); // just feed it the contents verbatim
    binWriter.Close();

the above code does not make any changes to the incoming byte stream and produces identical files when I run it through WinMerge

As comments suggest, you might be better off just copying the file altogether:

    string fileNameWithPath_ = "1.pwpmi";
    string newfileNameWithPath_ = "2.pwpmi";
    File.Copy(fileNameWithPath_, newfileNameWithPath_, overwrite: true);

The .NET framework provides a built-in method to copy a file:

File.Copy(fileNameWithPath_, newfileNameWithPath_)

(Here, File is System.IO.File .)

Or alternatively:

using (FileStream inStream = new FileStream(fileNameWithPath_, FileMode.Open, FileAccess.Read))
using (FileStream outStream = new FileStream(newfileNameWithPath_, FileMode.Create, FileAccess.Write))
{
    inStream.CopyTo(outStream);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM