tags:

views:

38

answers:

2

Hello. How can I write Byte [] buf to the file without turning it into string ?

public Int32 OnWriteData(Byte[] buf, Int32 size, Int32 nmemb, Object extraData)
{
    SockBuff = SockBuff + System.Text.Encoding.UTF8.GetString(buf);
    return size * nmemb;
}

Been trying to find something working for hours now.

+2  A: 

System.IO.File.WriteAllBytes

MCain
A: 

Use the above answer if you just want a single file with nothing but your byte buffer, if you want to do multiple byte buffer writes to one file though, I write bytes like so:

using (StreamWriter myStreamWriter = new StreamWriter(filePath))
{
    // Some logic or something, who knows
    myStreamWriter.BaseStream.Write(byteBuffer1, 0, byteBuffer1.Length);
    // Some logic or something, who knows
    myStreamWriter.BaseStream.Write(byteBuffer2, 0, byteBuffer2.Length);
}
Jimmy Hoffa
Worked great!!!
@user444434: Just remember when writing to the base stream, that if you write to the StreamWriter itself you'll need to flush, or else the StreamWriters internal buffer and it's base stream can get out of sync.
Jimmy Hoffa