1

I would like to append an Byte Array to an existing File. It must be at the end of the File. I could already manage to write at the start of the file. (Thanks to stackoverflow ;)).

Code for that:

public bool ByteArrayToFile(string _FileName, byte[] _ByteArray)
{
   try
   {
      // Open file for reading
      System.IO.FileStream _FileStream = 
         new System.IO.FileStream(_FileName, System.IO.FileMode.Create,
                                  System.IO.FileAccess.Write);
  // Writes a block of bytes to this stream using data from
  // a byte array.
  _FileStream.Write(_ByteArray, 0, _ByteArray.Length);

  // close file stream
  _FileStream.Close();

  return true;
   }
catch (Exception _Exception)
{
  // Error
  Console.WriteLine("Exception caught in process: {0}",
                    _Exception.ToString());
}

// error occured, return false
return false;

}

Got it from here:

Link

But I need it at the end of the file

Thanks in advance.

Found the solution:

FileStream writeStream;
        try
        {
            writeStream = new FileStream(_FileName, FileMode.Append,FileAccess.Write);
            BinaryWriter writeBinay = new BinaryWriter(writeStream);
            writeBinay.Write(_ByteArray);
            writeBinay.Close();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
Community
  • 1
  • 1

1 Answers1

5

Instead of using System.IO.FileMode.Create, use System.IO.FileMode.Append - it does exactly what you need.

From FileMode Enumeration on MSDN:

Append: Opens the file if it exists and seeks to the end of the file, or creates a new file. This requires FileIOPermissionAccess.Append permission. FileMode.Append can be used only in conjunction with FileAccess.Write. Trying to seek to a position before the end of the file throws an IOException exception, and any attempt to read fails and throws a NotSupportedException exception.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • I tried it already. Didn't work. Atleast for me. Could you give me some Code which work for you? – Michael Salivon Mar 31 '13 at 12:43
  • It has thrown me the error, that the Index was out of bounds or offsets weren't right. – Michael Salivon Apr 01 '13 at 09:07
  • @MichaelSalivon - If you want to append, don't start with index 0 of the stream, but from the last index of the existing file. – Oded Apr 01 '13 at 09:11
  • And thats where my Problem Comes again, how can I figure out where the last Offset is, and what I should write in this function: _FileStream.Write(x, ?, ?); – Michael Salivon Apr 01 '13 at 09:14
  • I tried: _FileStream.Write(_ByteArray, _FIleStream.Lenght, _ByteArray.Length); Of COurse with FileMode.Append. – Michael Salivon Apr 01 '13 at 11:40
  • @MichaelSalivon - Remember that indexes start at 0. The end of the file byte array would be `_FIleStream.Length - 1`. – Oded Apr 01 '13 at 11:41
  • Thank you, I could do it now. Instead of using Filestream I used BinaryWriter and .Append. See my first post for solution. – Michael Salivon Apr 02 '13 at 10:48
  • What are you talking about!? The second parameter in FileStream.Write is the position inside the input byte array. – IOviSpot Nov 25 '21 at 00:51