/*17.4
(序列化和去序列化)修改前面的程序,使其能利用可以被序列化和去序列化的类对象
*/
using System;
[ Serializable ]
class ClassGrade
{
public string Last { get; set; }
public string First { get; set; }
public string Id { get; set; }
public string Class { get; set; }
public string Grade { get; set; }
public ClassGrade()
{
}
public ClassGrade( string lastName, string firstName,
string id, string className, string grade )
{
Last = lastName;
First = firstName;
Id = id;
Class = className;
Grade = grade;
}
}
using System;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace Grades
{
public partial class GradesForm : Form
{
private FileStream fileInOut;
private bool saved = false;
private BinaryFormatter formatter;
public GradesForm()
{
InitializeComponent();
formatter = new BinaryFormatter();
}
private void AllTextBox_TextChanged( object sender, EventArgs e )
{
EnableEnterButton();
}
private void saveAsButton_Click( object sender, EventArgs e )
{
if ( fileInOut != null )
{
fileInOut.Close();
fileInOut = null;
statusLabel.Text = "Closing file";
}
DialogResult result;
string fileName;
using ( SaveFileDialog chooser = new SaveFileDialog() )
{
result = chooser.ShowDialog();
fileName = chooser.FileName;
}
if ( result == DialogResult.OK )
{
if ( fileName == string.Empty )
MessageBox.Show( "Invalid File Name", "Invalid File name",
MessageBoxButtons.OK, MessageBoxIcon.Information );
else
{
fileInOut = new FileStream( fileName,
FileMode.OpenOrCreate );
saveAsButton.Enabled = false;
saved = true;
}
EnableEnterButton();
}
}
private void EnableEnterButton()
{
if ( lastTextBox.Text != string.Empty &&
firstTextBox.Text != string.Empty &&
idTextBox.Text != string.Empty &&
classTextBox.Text != string.Empty &&
gradeTextBox.Text != string.Empty && saved )
enterButton.Enabled = true;
else
enterButton.Enabled = false;
}
private void enterButton_Click( object sender, EventArgs e )
{
string last = lastTextBox.Text;
string first = firstTextBox.Text;
string id = idTextBox.Text;
string className = classTextBox.Text;
string grade = gradeTextBox.Text;
ClassGrade entry = new ClassGrade( last, first, id,
className, grade );
formatter.Serialize( fileInOut, entry );
statusLabel.Text = "Entry saved";
lastTextBox.Clear();
firstTextBox.Clear();
idTextBox.Clear();
classTextBox.Clear();
gradeTextBox.Clear();
}
private void loadButton_Click( object sender, EventArgs e )
{
if ( fileInOut != null )
{
fileInOut.Close();
fileInOut = null;
statusLabel.Text = "Closing file";
saveAsButton.Enabled = true;
saved = false;
}
DialogResult result;
string fileName;
using ( OpenFileDialog fileChooser = new OpenFileDialog() )
{
result = fileChooser.ShowDialog();
fileName = fileChooser.FileName;
}
if ( result == DialogResult.OK )
{
if ( fileName == string.Empty )
MessageBox.Show( "Invalid File Name", "Invalid File name",
MessageBoxButtons.OK, MessageBoxIcon.Information );
else
{
fileInOut = new FileStream( fileName,
FileMode.Open, FileAccess.Read );
gradesTextBox.Clear();
try
{
while ( true )
{
ClassGrade entry =
( ClassGrade ) formatter.Deserialize( fileInOut );
gradesTextBox.AppendText( FormatEntry( entry ) +
"\r\n" );
}
}
catch ( SerializationException )
{
statusLabel.Text = "File loaded";
}
}
}
}
private string FormatEntry( ClassGrade fromFile )
{
string result = fromFile.Last.Trim() + ", " +
fromFile.First.Trim() + ":\t" + fromFile.Id.Trim() + "\t" +
fromFile.Class.Trim() + "\t" + fromFile.Grade.Trim();
return result;
}
}
} using System;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace Grades
{
public partial class GradesForm : Form
{
private FileStream fileInOut;
private bool saved = false;
private BinaryFormatter formatter;
public GradesForm()
{
InitializeComponent();
formatter = new BinaryFormatter();
}
private void AllTextBox_TextChanged( object sender, EventArgs e )
{
EnableEnterButton();
}
private void saveAsButton_Click( object sender, EventArgs e )
{
if ( fileInOut != null )
{
fileInOut.Close();
fileInOut = null;
statusLabel.Text = "Closing file";
}
DialogResult result;
string fileName;
using ( SaveFileDialog chooser = new SaveFileDialog() )
{
result = chooser.ShowDialog();
fileName = chooser.FileName;
}
if ( result == DialogResult.OK )
{
if ( fileName == string.Empty )
MessageBox.Show( "Invalid File Name", "Invalid File name",
MessageBoxButtons.OK, MessageBoxIcon.Information );
else
{
fileInOut = new FileStream( fileName,
FileMode.OpenOrCreate );
saveAsButton.Enabled = false;
saved = true;
}
EnableEnterButton();
}
}
private void EnableEnterButton()
{
if ( lastTextBox.Text != string.Empty &&
firstTextBox.Text != string.Empty &&
idTextBox.Text != string.Empty &&
classTextBox.Text != string.Empty &&
gradeTextBox.Text != string.Empty && saved )
enterButton.Enabled = true;
else
enterButton.Enabled = false;
}
private void enterButton_Click( object sender, EventArgs e )
{
string last = lastTextBox.Text;
string first = firstTextBox.Text;
string id = idTextBox.Text;
string className = classTextBox.Text;
string grade = gradeTextBox.Text;
ClassGrade entry = new ClassGrade( last, first, id,
className, grade );
formatter.Serialize( fileInOut, entry );
statusLabel.Text = "Entry saved";
lastTextBox.Clear();
firstTextBox.Clear();
idTextBox.Clear();
classTextBox.Clear();
gradeTextBox.Clear();
}
private void loadButton_Click( object sender, EventArgs e )
{
if ( fileInOut != null )
{
fileInOut.Close();
fileInOut = null;
statusLabel.Text = "Closing file";
saveAsButton.Enabled = true;
saved = false;
}
DialogResult result;
string fileName;
using ( OpenFileDialog fileChooser = new OpenFileDialog() )
{
result = fileChooser.ShowDialog();
fileName = fileChooser.FileName;
}
if ( result == DialogResult.OK )
{
if ( fileName == string.Empty )
MessageBox.Show( "Invalid File Name", "Invalid File name",
MessageBoxButtons.OK, MessageBoxIcon.Information );
else
{
fileInOut = new FileStream( fileName,
FileMode.Open, FileAccess.Read );
gradesTextBox.Clear();
try
{
while ( true )
{
ClassGrade entry =
( ClassGrade ) formatter.Deserialize( fileInOut );
gradesTextBox.AppendText( FormatEntry( entry ) +
"\r\n" );
}
}
catch ( SerializationException )
{
statusLabel.Text = "File loaded";
}
}
}
}
private string FormatEntry( ClassGrade fromFile )
{
string result = fromFile.Last.Trim() + ", " +
fromFile.First.Trim() + ":\t" + fromFile.Id.Trim() + "\t" +
fromFile.Class.Trim() + "\t" + fromFile.Grade.Trim();
return result;
}
}
}
原文地址:https://www.cnblogs.com/hsyv123ve/p/8734184.html