using System;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using BankLibrary;namespace CreateFile
{
public partial class CreateFileForm : BankUIForm
{
// object for serializing Records in binary format
private BinaryFormatter formatter = new BinaryFormatter();
private FileStream output; // stream for writing to a file // parameterless constructor
public CreateFileForm()
{
InitializeComponent();
} // end constructor // handler for saveButton_Click
private void saveButton_Click( object sender, EventArgs e )
{
// create and show dialog box enabling user to save file
DialogResult result;
string fileName; // name of file to save data using ( SaveFileDialog fileChooser = new SaveFileDialog() )
{
fileChooser.CheckFileExists = false; // let user create file // retrieve the result of the dialog box
result = fileChooser.ShowDialog();
fileName = fileChooser.FileName; // get specified file name
} // end using // ensure that user clicked "OK"
if ( result == DialogResult.OK )
{ // show error if user specified invalid file
if ( fileName == string.Empty )
MessageBox.Show( "Invalid File Name", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error );
else
{
// save file via FileStream if user specified valid file
try
{
// open file with write access
output = new FileStream( fileName,
FileMode.OpenOrCreate, FileAccess.Write ); // disable Save button and enable Enter button
saveButton.Enabled = false;
enterButton.Enabled = true;
} // end try
// handle exception if there is a problem opening the file
catch ( IOException )
{
// notify user if file could not be opened
MessageBox.Show( "Error opening file", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error );
} // end catch
} // end else
} // end if
} // end method saveButton_Click // handler for enterButton Click
private void enterButton_Click( object sender, EventArgs e )
{
// store TextBox values string array
string[] values = GetTextBoxValues(); // Record containing TextBox values to serialize
RecordSerializable record = new RecordSerializable(); // determine whether TextBox account field is empty
if ( values[ ( int ) TextBoxIndices.ACCOUNT ] != string.Empty )
{
// store TextBox values in Record and serialize Record
try
{
// get account number value from TextBox
int accountNumber = Int32.Parse(
values[ ( int ) TextBoxIndices.ACCOUNT ] ); // determine whether accountNumber is valid
if ( accountNumber > 0 )
{
// store TextBox fields in Record
record.Account = accountNumber;
record.FirstName = values[ ( int )
TextBoxIndices.FIRST ];
record.LastName = values[ ( int )
TextBoxIndices.LAST ];
record.Balance = Decimal.Parse( values[
( int ) TextBoxIndices.BALANCE ] ); // write Record to FileStream ( serialize object )
formatter.Serialize( output, record );
} // end if
else
{
// notify user if invalid account number
MessageBox.Show( "Invalid Account Number", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error );
} // end else
} // end try
// notify user if error occurs in serialization
catch ( SerializationException )
{
MessageBox.Show( "Error Writing to File", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error );
} // end catch
// notify user if error occurs regarding parameter format
catch ( FormatException )
{
MessageBox.Show( "Invalid Format", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error );
} // end catch
} // end if ClearTextBoxes(); // clear TextBox values
} // end method enterButton_Click // handler for exitButton Click
private void exitButton_Click( object sender, EventArgs e )
{
// determine whether file exists
if ( output != null )
{
// close file
try
{
output.Close(); // close FileStream
} // end try
// notify user of error closing file
catch ( IOException )
{
MessageBox.Show( "Cannot close file", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error );
} // end catch
} // end if Application.Exit();
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using BankLibrary;namespace CreateFile
{
public partial class CreateFileForm : BankUIForm
{
// object for serializing Records in binary format
private BinaryFormatter formatter = new BinaryFormatter();
private FileStream output; // stream for writing to a file // parameterless constructor
public CreateFileForm()
{
InitializeComponent();
} // end constructor // handler for saveButton_Click
private void saveButton_Click( object sender, EventArgs e )
{
// create and show dialog box enabling user to save file
DialogResult result;
string fileName; // name of file to save data using ( SaveFileDialog fileChooser = new SaveFileDialog() )
{
fileChooser.CheckFileExists = false; // let user create file // retrieve the result of the dialog box
result = fileChooser.ShowDialog();
fileName = fileChooser.FileName; // get specified file name
} // end using // ensure that user clicked "OK"
if ( result == DialogResult.OK )
{ // show error if user specified invalid file
if ( fileName == string.Empty )
MessageBox.Show( "Invalid File Name", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error );
else
{
// save file via FileStream if user specified valid file
try
{
// open file with write access
output = new FileStream( fileName,
FileMode.OpenOrCreate, FileAccess.Write ); // disable Save button and enable Enter button
saveButton.Enabled = false;
enterButton.Enabled = true;
} // end try
// handle exception if there is a problem opening the file
catch ( IOException )
{
// notify user if file could not be opened
MessageBox.Show( "Error opening file", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error );
} // end catch
} // end else
} // end if
} // end method saveButton_Click // handler for enterButton Click
private void enterButton_Click( object sender, EventArgs e )
{
// store TextBox values string array
string[] values = GetTextBoxValues(); // Record containing TextBox values to serialize
RecordSerializable record = new RecordSerializable(); // determine whether TextBox account field is empty
if ( values[ ( int ) TextBoxIndices.ACCOUNT ] != string.Empty )
{
// store TextBox values in Record and serialize Record
try
{
// get account number value from TextBox
int accountNumber = Int32.Parse(
values[ ( int ) TextBoxIndices.ACCOUNT ] ); // determine whether accountNumber is valid
if ( accountNumber > 0 )
{
// store TextBox fields in Record
record.Account = accountNumber;
record.FirstName = values[ ( int )
TextBoxIndices.FIRST ];
record.LastName = values[ ( int )
TextBoxIndices.LAST ];
record.Balance = Decimal.Parse( values[
( int ) TextBoxIndices.BALANCE ] ); // write Record to FileStream ( serialize object )
formatter.Serialize( output, record );
} // end if
else
{
// notify user if invalid account number
MessageBox.Show( "Invalid Account Number", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error );
} // end else
} // end try
// notify user if error occurs in serialization
catch ( SerializationException )
{
MessageBox.Show( "Error Writing to File", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error );
} // end catch
// notify user if error occurs regarding parameter format
catch ( FormatException )
{
MessageBox.Show( "Invalid Format", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error );
} // end catch
} // end if ClearTextBoxes(); // clear TextBox values
} // end method enterButton_Click // handler for exitButton Click
private void exitButton_Click( object sender, EventArgs e )
{
// determine whether file exists
if ( output != null )
{
// close file
try
{
output.Close(); // close FileStream
} // end try
// notify user of error closing file
catch ( IOException )
{
MessageBox.Show( "Cannot close file", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error );
} // end catch
} // end if Application.Exit();
解决方案 »
- 菜鸟:C#里引用C++dll,出现“尝试读取或写入受保护的内存。这通常指示其他内存已损坏”错误,附上所有源码求帮助。
- 大家好,我问两个TreeView控件的小问题
- KeyDown事件的用法!!!
- asp.net有没有类似ASP里的Response.End 语句?
- 2005怎样在读写app.config文件?
- 如何在在动态生成的表格跟按钮中实现分页功能
- 如何去掉IE的状态栏
- 线程问题
- 问高手一个问题,请问如何从数据库的一个表中随机的抽取一条(比如,一个试题库,怎么随机的抽取一道试题),我看了MSDN的random class。
- 春节期间把C# 猛啃了一下,想的个简单项目练练手
- web截屏
- 怎样快速用GDI+绘制大量的小图片?
{
错误 1 找不到类型或命名空间名称“BankLibrary”(是否缺少 using 指令或程序集引用?) G:\作业\c#\课程答案\ch19\Fig19_14\CreateFile\CreateFile\CreateFileForm.cs 8 7 CreateFile错误 2 找不到类型或命名空间名称“BankUIForm”(是否缺少 using 指令或程序集引用?) G:\作业\c#\课程答案\ch19\Fig19_14\CreateFile\CreateFile\CreateFileForm.cs 12 42 CreateFile
警告 3 Could not resolve this reference. Could not locate the assembly "BankLibrary". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors. CreateFile
警告 4 未能找到引用的组件“BankLibrary”。
2:你直接复制黏贴了别人代码吧?人家的类库你没弄来,或者弄来了引用了你没有在一开始using他的命名空间