Click here to Skip to main content
15,903,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone, this is my first post. I am from Argentina, so I'll try to do the best out of my english.

I need help with the following issue. I receive a file from a client application to a server application using NetworkStream. As I receive the file, I save it temporarily to disc in a dile called "temp". The recepcion is succesfull, every byte is receive perfectly.
The protocol I used to send the file is (without "): "FileName|FileSize|FileContent"
Now I need to make up the final file, the original. I can create a new file using FileStream with the name receive in the temp file, as well as I can read its size, but I don't know how to write only the "temp" FileContent in the new file. Is there any simple way to exclude the FileName and FileSize?
Posted

Thanks for answering.
I resolve the issue in the following way: After receiving and storing the temp file, I read 1024 bytes to a buffer which I convert to string, then with string.spit('|') I separate the name and file size in different variables. After it, with file.seek I positioned the reding pointer to the first byte after "FileSize|" and save all the readings to a new final file.

I have read the articles you recomend. Very interesting by the way.
It would be great to be able to remove the FileName|FileSize bytes of the temporary file after reading them, and then just rename the temp file to become the original file. I didn't find how to do this, nor if it is posible.
Thanks!!
 
Share this answer
 
Comments
belit 25-Apr-12 4:01am    
I have similar Question, please help me guys.
I use the filestream reader/writer in C# to read files and save them in another file name/folder. It works perfect for .doc, .txt, .pdf files but for .html file.
when I opened the html file with images, and then saves to another file, the imbedded images are not displayed, what do u think the reason is?
Here is the sample code I used
//open the file and stores the byte streams in array
public byte [] openfile()
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.InitialDirectory = @"C:\";

if (dlg.ShowDialog() == DialogResult.OK)
{
m_isOpenflag = true;

m_fileName = dlg.FileName; ;


FileStream fs = new FileStream(Convert.ToString(m_fileName), FileMode.Open, FileAccess.Read);
BinaryReader breader = new BinaryReader(fs);

m_byteArray = breader.ReadBytes((int)fs.Length);

breader.Close();
fs.Close();

return m_byteArray;
}

else
return null;
}

//saves the byte streams in array into a file
public void Write()
{
DialogResult ck = new DialogResult();
SaveFileDialog saveFile = new SaveFileDialog();
ck = saveFile.ShowDialog();

if (ck == DialogResult.OK)
{
filePath = saveFile.FileName;

FileStream fs1 = new FileStream(filePath, FileMode.Create);
BinaryWriter sr1 = new BinaryWriter(fs1);

sr1.Write(m_byteArray);

sr1.Close();
fs1.Close();
MessageBox.Show("It is Successfully written in" + filePath);
//recoveredChar = null;
}


else
MessageBox.Show("The message is not saved");

}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900