Skip to content

C# writing to a specific byte in a binary file

jim

Asked Aug 15, 2007, 7:04 PM EDT1 answer
Share
LinkedInX
how do i write to a overwrite a specific byte in a binary file? i looked around, but i couldn't figure it out.

i figured out how to write to the file, but it write it at the beginning of the file, is there anyway to write it in the middle of the file? or a specified location?

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • Mike Gold
    0

    I think you just need to open the file using the FileStream class for writing,  set the Position property you want to write to, call

    myFileStream.Position = 121;

    or you can call Seek

    Then to write to the file

    C#
    public override void Write (
    	byte[] array,
    	int offset,
    	int count
    )
    Code
    if you are only writing one byte, then call
    Code
    myFileStream.Write(new byte[]{abyte}, 0, 1);
    Code
    Don't forget to close the filestream when you are finished.
    0