Problem - Serializing ArrayList After OpenFileDialog

  • Thread starter Thread starter Dylan Spence
  • Start date Start date
D

Dylan Spence

This is a sample application of the problem.

I need to serialize an arraylist of strings and read it back in. It
works, except for when I have the user select file from the
OpenFileDialog. The string returned ('FileName') will not serialize
out, eventhough it shows up in the collection. I have tried manually
copying it character by character with no luck. This seems like a
reference issue, but I cannot find it.

Does anyone know how to fix this or how to get around it? I saw one
previous post for this problem but no solution. Using 1.0 of
framework. This really puts a stick into the whole application
because a large part of it rests on the user picking files and then
serializing that lists of files.

Summary ---
Three buttons on the form.
First adds from typed in input from textbox 1 -- this works
Second adds input from the openfiledialog -- this works (does not
serialize)
Third adds input from code -- this works

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

namespace WindowsApplication1
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private ArrayList list;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.ListBox textBox2;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Label label1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent
call
//
if (File.Exists("mytest") == true) {
FileStream stream = File.OpenRead("mytest");
IFormatter formatter = new BinaryFormatter();
this.list = (ArrayList) formatter.Deserialize(stream);
this.Form_InitList();
}
else
this.list = new ArrayList();

this.Closing += new CancelEventHandler(this.Form_Closing);
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.ListBox();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(160, 104);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(136, 23);
this.button1.TabIndex = 0;
this.button1.Text = "Open File";
this.button1.Click += new
System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(24, 64);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 1;
this.textBox1.Text = "textBox1";
//
// textBox2
//
this.textBox2.ItemHeight = 16;
this.textBox2.Location = new System.Drawing.Point(24, 104);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(100, 116);
this.textBox2.TabIndex = 2;
//
// button2
//
this.button2.Location = new System.Drawing.Point(160, 136);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(136, 23);
this.button2.TabIndex = 3;
this.button2.Text = "Add From Code";
this.button2.Click += new
System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(160, 72);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(136, 23);
this.button3.TabIndex = 4;
this.button3.Text = "Add From TextBox1";
this.button3.Click += new
System.EventHandler(this.button3_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(160, 48);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(112, 23);
this.label1.TabIndex = 5;
this.label1.Text = "Add To ArrayList";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
this.ClientSize = new System.Drawing.Size(304, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {

this.label1,

this.button3,

this.button2,

this.textBox2,

this.textBox1,

this.button1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e) {
OpenFileDialog of = new OpenFileDialog();
if (of.ShowDialog() == DialogResult.OK) {
this.textBox1.Text = of.FileName;
if (this.list.Contains(of.FileName) == false)
//this.list.Add(String.Copy(of.FileName));
this.list.Add(of.FileName);
this.Form_InitList();
}
of.Dispose();
}

private void Form_Closing(object sender, CancelEventArgs e) {
FileStream stream = File.Open("mytest", FileMode.Create,
FileAccess.ReadWrite);
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, this.list);
}

private void Form_InitList() {
this.textBox2.Items.Clear();
foreach(string s in this.list)
this.textBox2.Items.Add(s);
}

private void button2_Click(object sender, System.EventArgs e) {
this.list.Add(DateTime.Now.ToString());
this.Form_InitList();

}

private void button3_Click(object sender, System.EventArgs e) {
this.list.Add(this.textBox1.Text);
this.Form_InitList();
}
}
}
 
Hi, Dylan:
The problem is that if you open a openfiledialog, and select a
file, and current path of this application has changed according to your
selection. The serialization is successful and the binary file is generated,
but it is saved to a different location(By default, the location would be
Documents and Setting/user/My documents). It is a very interesting trick :-)


Qiu

Dylan Spence said:
This is a sample application of the problem.

I need to serialize an arraylist of strings and read it back in. It
works, except for when I have the user select file from the
OpenFileDialog. The string returned ('FileName') will not serialize
out, eventhough it shows up in the collection. I have tried manually
copying it character by character with no luck. This seems like a
reference issue, but I cannot find it.

Does anyone know how to fix this or how to get around it? I saw one
previous post for this problem but no solution. Using 1.0 of
framework. This really puts a stick into the whole application
because a large part of it rests on the user picking files and then
serializing that lists of files.

Summary ---
Three buttons on the form.
First adds from typed in input from textbox 1 -- this works
Second adds input from the openfiledialog -- this works (does not
serialize)
Third adds input from code -- this works

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

namespace WindowsApplication1
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private ArrayList list;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.ListBox textBox2;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Label label1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent
call
//
if (File.Exists("mytest") == true) {
FileStream stream = File.OpenRead("mytest");
IFormatter formatter = new BinaryFormatter();
this.list = (ArrayList) formatter.Deserialize(stream);
this.Form_InitList();
}
else
this.list = new ArrayList();

this.Closing += new CancelEventHandler(this.Form_Closing);
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.ListBox();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(160, 104);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(136, 23);
this.button1.TabIndex = 0;
this.button1.Text = "Open File";
this.button1.Click += new
System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(24, 64);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 1;
this.textBox1.Text = "textBox1";
//
// textBox2
//
this.textBox2.ItemHeight = 16;
this.textBox2.Location = new System.Drawing.Point(24, 104);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(100, 116);
this.textBox2.TabIndex = 2;
//
// button2
//
this.button2.Location = new System.Drawing.Point(160, 136);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(136, 23);
this.button2.TabIndex = 3;
this.button2.Text = "Add From Code";
this.button2.Click += new
System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(160, 72);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(136, 23);
this.button3.TabIndex = 4;
this.button3.Text = "Add From TextBox1";
this.button3.Click += new
System.EventHandler(this.button3_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(160, 48);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(112, 23);
this.label1.TabIndex = 5;
this.label1.Text = "Add To ArrayList";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
this.ClientSize = new System.Drawing.Size(304, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {

this.label1,

this.button3,

this.button2,

this.textBox2,

this.textBox1,

this.button1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e) {
OpenFileDialog of = new OpenFileDialog();
if (of.ShowDialog() == DialogResult.OK) {
this.textBox1.Text = of.FileName;
if (this.list.Contains(of.FileName) == false)
//this.list.Add(String.Copy(of.FileName));
this.list.Add(of.FileName);
this.Form_InitList();
}
of.Dispose();
}

private void Form_Closing(object sender, CancelEventArgs e) {
FileStream stream = File.Open("mytest", FileMode.Create,
FileAccess.ReadWrite);
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, this.list);
}

private void Form_InitList() {
this.textBox2.Items.Clear();
foreach(string s in this.list)
this.textBox2.Items.Add(s);
}

private void button2_Click(object sender, System.EventArgs e) {
this.list.Add(DateTime.Now.ToString());
this.Form_InitList();

}

private void button3_Click(object sender, System.EventArgs e) {
this.list.Add(this.textBox1.Text);
this.Form_InitList();
}
}
}
 
I copied your exact code into a new solution, and ran it. I received no
errors. I picked my desktop.ini file from my documents.

I ran this using VS 2003, .NET framework 1.1, on Windows XP with all service
packs and critical updates.

I did see a problem. You are not specifying the path to the file when you
open the file. At application start it looks for "myFile" in the folder
where the EXE is running. At application close it is opening the same file
for write, but in the folder of the file I added to the list. Then the next
time I ran it, it did not find the serialized file.

You should really specify a path also when opening a file. If you want it
in the application folder, then specify as such. At application load read
"Environment.CurrentDirectory" and save it to a string variable. At that
time it will be the application folder. If a user browses for a folder with
the file dialog, "Environment.CurrentDirectory" changes to the selected
folder.

Michael Lang, MCSD


Dylan Spence said:
This is a sample application of the problem.

I need to serialize an arraylist of strings and read it back in. It
works, except for when I have the user select file from the
OpenFileDialog. The string returned ('FileName') will not serialize
out, eventhough it shows up in the collection. I have tried manually
copying it character by character with no luck. This seems like a
reference issue, but I cannot find it.

Does anyone know how to fix this or how to get around it? I saw one
previous post for this problem but no solution. Using 1.0 of
framework. This really puts a stick into the whole application
because a large part of it rests on the user picking files and then
serializing that lists of files.

Summary ---
Three buttons on the form.
First adds from typed in input from textbox 1 -- this works
Second adds input from the openfiledialog -- this works (does not
serialize)
Third adds input from code -- this works

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

namespace WindowsApplication1
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private ArrayList list;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.ListBox textBox2;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Label label1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent
call
//
if (File.Exists("mytest") == true) {
FileStream stream = File.OpenRead("mytest");
IFormatter formatter = new BinaryFormatter();
this.list = (ArrayList) formatter.Deserialize(stream);
this.Form_InitList();
}
else
this.list = new ArrayList();

this.Closing += new CancelEventHandler(this.Form_Closing);
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.ListBox();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(160, 104);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(136, 23);
this.button1.TabIndex = 0;
this.button1.Text = "Open File";
this.button1.Click += new
System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(24, 64);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 1;
this.textBox1.Text = "textBox1";
//
// textBox2
//
this.textBox2.ItemHeight = 16;
this.textBox2.Location = new System.Drawing.Point(24, 104);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(100, 116);
this.textBox2.TabIndex = 2;
//
// button2
//
this.button2.Location = new System.Drawing.Point(160, 136);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(136, 23);
this.button2.TabIndex = 3;
this.button2.Text = "Add From Code";
this.button2.Click += new
System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(160, 72);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(136, 23);
this.button3.TabIndex = 4;
this.button3.Text = "Add From TextBox1";
this.button3.Click += new
System.EventHandler(this.button3_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(160, 48);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(112, 23);
this.label1.TabIndex = 5;
this.label1.Text = "Add To ArrayList";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
this.ClientSize = new System.Drawing.Size(304, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {

this.label1,

this.button3,

this.button2,

this.textBox2,

this.textBox1,

this.button1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e) {
OpenFileDialog of = new OpenFileDialog();
if (of.ShowDialog() == DialogResult.OK) {
this.textBox1.Text = of.FileName;
if (this.list.Contains(of.FileName) == false)
//this.list.Add(String.Copy(of.FileName));
this.list.Add(of.FileName);
this.Form_InitList();
}
of.Dispose();
}

private void Form_Closing(object sender, CancelEventArgs e) {
FileStream stream = File.Open("mytest", FileMode.Create,
FileAccess.ReadWrite);
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, this.list);
}

private void Form_InitList() {
this.textBox2.Items.Clear();
foreach(string s in this.list)
this.textBox2.Items.Add(s);
}

private void button2_Click(object sender, System.EventArgs e) {
this.list.Add(DateTime.Now.ToString());
this.Form_InitList();

}

private void button3_Click(object sender, System.EventArgs e) {
this.list.Add(this.textBox1.Text);
this.Form_InitList();
}
}
}
 
Back
Top