蛋碎了,这是什么情况!

龙在江湖 龙在江湖 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
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!这个异常的意思是:在序列化或反序列化期间出错引发的异常!怎么解决啊!对于高手应该不是什么大问题吧!给高分悬赏!
...全文

相关推荐

评论 25
收藏 已收藏
分享
返回顶部
龙在江湖
.NET社区 62,275 成员 668,967 内容