码迷,mamicode.com
首页 > Windows程序 > 详细

C#大学课程(第五版)课后习题17.4序列化和去序列化

时间:2018-04-07 20:02:00      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:catch   .sh   change   box   trim   names   dia   set   runtime   

/*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;
}
}
}

C#大学课程(第五版)课后习题17.4序列化和去序列化

标签:catch   .sh   change   box   trim   names   dia   set   runtime   

原文地址:https://www.cnblogs.com/hsyv123ve/p/8734184.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!