蛋碎了,这是什么情况!
龙在江湖 2011-09-10 03:56:44 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using BankLibrary;
namespace CreatFileFormClass
{
public partial class CreatFileForm : BankLibrary.BankUIForm
{
private BinaryFormatter formatter = new BinaryFormatter();
private FileStream output;
public CreatFileForm()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void saveButton_Click(object sender, EventArgs e)
{
SaveFileDialog fileChosser = new SaveFileDialog();
DialogResult result = fileChosser.ShowDialog();
string fileName;
fileChosser.CheckFileExists = false;
if (result == DialogResult.Cancel)
return;
fileName = fileChosser.FileName;
if (fileName == "" || fileName == null)
MessageBox.Show("Invalid file Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
else
{
try
{
output = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);
saveButton.Enabled = false;
enterButton.Enabled = true;
}
catch(FileNotFoundException)
{
MessageBox.Show("File Dose Not Exits", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
private void enterButton_Click(object sender, EventArgs e)
{
string[] values = GetTextBoxValues();
Record record = new Record();
if (values[(int)TextBoxIndices.ACCOUNT] != "")
{
try
{
int accountNumber = Int32.Parse(values[(int)TextBoxIndices.ACCOUNT]);
if (accountNumber > 0)
{
record.Account = accountNumber;
record.FirstName = values[(int)TextBoxIndices.FIRST];
record.LastName = values[(int)TextBoxIndices.LAST];
record.Balance = Double.Parse(values[(int)TextBoxIndices.BALANCE]);
formatter.Serialize(output, record);
}
else
{
MessageBox.Show("Invalid Account Number", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (SerializationException)
{
MessageBox.Show("Error writing to file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (FormatException)
{
MessageBox.Show("Invalid Format", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
ClearTextBoxs();
}
private void exitButton_Click(object sender, EventArgs e)
{
if (output == null)
{
try
{ output.Close(); }
catch (IOException)
{
MessageBox.Show("Canot Exit Files", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
Application.Exit();
}
}
}
这个蛋疼啊!代码没有错误!运行后新建了一个文件用于保存文件,然后输入数据点保存 出现异常!serializationException!这个异常的意思是:在序列化或反序列化期间出错引发的异常!怎么解决啊!对于高手应该不是什么大问题吧!给高分悬赏!