private void button3_Click(object sender, EventArgs e) { SubmitPaper yourFriendSPaper = DecodeFile(yourFriendFileName); SubmitPaper yourSPaper = DecodeFile(yourFileName); if (yourFriendSPaper != null && yourSPaper != null) { yourSPaper.SPaper = yourFriendSPaper.SPaper; yourSPaper.SubmitTime = DateTime.Now; EncodeFile(textBox2.Text, yourSPaper); } else { MessageBox.Show("Cannot decode file", "Error"); } MessageBox.Show("Create file successfuly"); }
private void EncodeFile(string fileName, SubmitPaper yourSPaper) { if ((fileName != null) && (fileName.Trim() != "")) { FileStream fs = new FileStream(fileName, FileMode.Create); BinaryFormatter formatter = new BinaryFormatter(); try { formatter.Serialize(fs, yourSPaper); } catch (Exception exception) { MessageBox.Show("Failed to create new file!\nReason: " + exception.Message, "Take an error!", MessageBoxButtons.OK, MessageBoxIcon.Hand); } finally { fs.Close(); } } }
private SubmitPaper DecodeFile(string fileName) { SubmitPaper c = null; if ((fileName != null) && (fileName.Trim() != "")) { FileStream serializationStream = new FileStream(fileName, FileMode.Open); try { BinaryFormatter formatter = new BinaryFormatter(); c = (SubmitPaper)formatter.Deserialize(serializationStream); } catch (Exception exception) { MessageBox.Show("Failed to load Sample!\nReason: " + exception.Message, "Take an error!", MessageBoxButtons.OK, MessageBoxIcon.Hand); } finally { serializationStream.Close(); } } return(c); }