using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.Serialization; using System.IO; using System.Runtime.Serialization.Formatters.Binary; namespace binary { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { IFormatter formater = new BinaryFormatter(); Stream stream = new FileStream( label3.Text.ToString(), FileMode.Create, FileAccess.Write, FileShare.None); formater.Serialize(stream, textBox1.Text.ToString()); stream.Close(); MessageBox.Show("写入文件完毕", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } private void button2_Click(object sender, EventArgs e) { IFormatter formater = new BinaryFormatter(); Stream stream = new FileStream( "test.dat", FileMode.Open, FileAccess.Read, FileShare.Read); string s = (string)formater.Deserialize(stream); label1.Text = s; stream.Close(); } } }