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();

解决方案 »

  1.   

    但是出现的效果是这样的
    {
    错误 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.   

    1:你编译的顺序错了 有时候虽然智能提示有但是编译顺序错了会出现这种情况
    2:你直接复制黏贴了别人代码吧?人家的类库你没弄来,或者弄来了引用了你没有在一开始using他的命名空间
      

  3.   

    Could not locate the assembly "BankLibrary". Check to make sure the assembly exists on disk,这句话都告诉你了,这些动态类都没找到。你找到这些类,重新引用一下。