[Mono-winforms-list] SerializationException not being caught

Paul paul at all-the-johnsons.co.uk
Sun Feb 25 10:19:29 EST 2007


Hi,

Can someone check to see if it's me or SerializationException not being
correctly caught in this code?

Attached is the datafile needed and lib.

I realise that sending a dll poses security risks and accept that some
may not wish to try the code.

Compiled it using gmcs, mono-1.2.3-1, Fedora rawhide.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;

using BankLibrary;

public class ReadSequentialAccessFileForm : BankUIForm
{
  private System.Windows.Forms.Button openbutton;
  private System.Windows.Forms.Button nextbutton;
  private System.ComponentModel.Container components = null;
  private FileStream input;
  private BinaryFormatter reader = new BinaryFormatter();

  [STAThread]
  static void Main()
  {
    Application.Run(new ReadSequentialAccessFileForm());
  }

  public ReadSequentialAccessFileForm()
  {
    InitializeComponent();
  }
  
  protected override void Dispose( bool disposing )
  {
    if( disposing )
    {
      if (components != null) 
      {
	components.Dispose();
      }
    }
    base.Dispose( disposing );
  }

  #region Windows Form Designer generated code
  
  private void InitializeComponent()
  {
    this.openbutton = new System.Windows.Forms.Button();
    this.nextbutton = new System.Windows.Forms.Button();
    this.SuspendLayout();
    
    this.accounttextbox.TabIndex = 1;    
    this.firstnametextbox.TabIndex = 2;    
    this.lastnametextbox.TabIndex = 3;
    this.balancetextbox.TabIndex = 4;
    
    this.openbutton.Font = new System.Drawing.Font("Microsoft Sans
Serif", 10F);
    this.openbutton.Location = new System.Drawing.Point(32, 232);
    this.openbutton.Name = "openButton";
    this.openbutton.Size = new System.Drawing.Size(144, 32);
    this.openbutton.TabIndex = 0;
    this.openbutton.Text = "Open File";
    this.openbutton.Click += new
System.EventHandler(this.openbutton_Click);
    
    this.nextbutton.Enabled = false;
    this.nextbutton.Font = new System.Drawing.Font("Microsoft Sans
Serif", 10F);
    this.nextbutton.Location = new System.Drawing.Point(216, 232);
    this.nextbutton.Name = "nextButton";
    this.nextbutton.Size = new System.Drawing.Size(144, 32);
    this.nextbutton.TabIndex = 5;
    this.nextbutton.Text = "Next Record";
    this.nextbutton.Click += new
System.EventHandler(this.nextbutton_Click);
    
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
    this.ClientSize = new System.Drawing.Size(400, 279);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
      this.balancetextbox,
      this.lastnametextbox,
      this.firstnametextbox,
      this.accounttextbox,
      this.balancelabel,
      this.lastnamelabel,
      this.firstnamelabel,
      this.accountlabel,
      this.nextbutton,
      this.openbutton});
    this.Name = "ReadSequentialAccessFileForm";
    this.Text = "Reading a Sequential File";
    this.ResumeLayout(false);
    
  }
  #endregion

  private void openbutton_Click(object s, System.EventArgs e)
  {
    OpenFileDialog filechooser = new OpenFileDialog();
    DialogResult result = filechooser.ShowDialog();
    string filename;
    if (result == DialogResult.Cancel)
      return;

    filename = filechooser.FileName;
    cleartextboxes();

    if (filename == "" || filename == null)
      MessageBox.Show("Invalid filename", "Error", MessageBoxButtons.OK,
		      MessageBoxIcon.Error);
    else
    {
      input = new FileStream(filename, FileMode.Open, FileAccess.Read);
      nextbutton.Enabled = true;
    }
  }

  private void nextbutton_Click(object s, System.EventArgs e)
  {
    try
    {
      Record record = (Record) reader.Deserialize(input);
      string [] values = new string[] {
	record.account.ToString(),
	record.firstname.ToString(),
	record.lastname.ToString(),
	record.balance.ToString() };
      settextboxvalues(values);
    }
    catch (SerializationException)
    {
      input.Close();
      openbutton.Enabled = true;
      nextbutton.Enabled = false;
      cleartextboxes();
      MessageBox.Show("No more records in file", "",
MessageBoxButtons.OK,
		      MessageBoxIcon.Information);
    }
  }
}


-- 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: BankUI.dll
Type: application/x-ms-dos-executable
Size: 8704 bytes
Desc: not available
Url : http://lists.ximian.com/pipermail/mono-winforms-list/attachments/20070225/dda042f1/attachment.bin 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: streams-test
Type: application/x-font-ttf
Size: 682 bytes
Desc: not available
Url : http://lists.ximian.com/pipermail/mono-winforms-list/attachments/20070225/dda042f1/attachment-0001.bin 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
Url : http://lists.ximian.com/pipermail/mono-winforms-list/attachments/20070225/dda042f1/attachment-0002.bin 


More information about the Mono-winforms-list mailing list