0

i want append 8 bytes of my info to first of stream in sending file. i use NetworkStream and my code is this:

        Stream Fs = File.OpenRead("filepath");
        Byte[] buffer = new Byte[Fs.Length];
        Fs.Read(buffer, 0, buffer.Length);
        NetworkStream serverStream = clientSocket.GetStream();
        serverStream.Write(buffer, 0, buffer.Length);
        serverStream.Close();

How can i add my strings? thanks

hahamed
  • 309
  • 1
  • 2
  • 12

1 Answers1

0

Simply add an extra Write call before the one you've got. Write the 8 bytes first and then write the file contents.

By the way, if you're going to read the entire file into a byte array then don't use those first three lines (which fail to close the FileStream by the way). Just call File.ReadAllBytes and do it all in one line.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
  • in your first answer i add `Fs.WriteByte` before `serverStream.Write` but i get error: Stream does not support writing. – hahamed Jan 25 '14 at 07:55
  • Why would you write to the FileStream, especially given that I told you to get rid of it? Do you want to change the file? No, you want the data written to the NetworkStream before the file contents so that's exactly what you do. Two calls to Write on the same stream: one to write this extra data and then one to write the file contents. – jmcilhinney Jan 25 '14 at 08:30