tags:

views:

41

answers:

1

Hello
I have a problem that I don't know how to solve.
I have have a Form that open a OpenFileDialog to add data.
But after I call OpenFileDialog.Show() I can't save anything with BinaryFormatter.Serialize.
The strange thing is that I doesn't even need to use the data that I have from the OpenFileDialog, only to open the dialog make the subsequent call to Serialize to stop working.

Stop working means that no new data is saved.

I don't get any error messages or exceptions, it just silently fail.
Do anyone have any pointers to when the problem could be.
I used the XmlSerializer when I added the OpenFileDialog and switched to BinaryFormatter.Serialize in hope that it should be more resistant to whatever problem I have so I'm pretty sure it affects XmlSerializer to.

I'm using .NET 4.0

Relevant code (I hope) is:
Save:

    public void Save()
    {
        File.Delete(fileName);
        using (Stream stream = File.Open(fileName, FileMode.Create))
        {
            BinaryFormatter bin = new BinaryFormatter();
            bin.Serialize(stream, contactList);
        }

    }

Load:

  private void LoadContactList()
    {
        if (File.Exists(fileName))
        {
            using (Stream stream = File.Open(fileName, FileMode.Open))
            {
                BinaryFormatter bin = new BinaryFormatter();
                contactList = (List<Contact>)bin.Deserialize(stream);
            }
        }
        else
        {
            // start on a new database
        }
   }

use of OpenFileDialog:

   private void btnPhoto_Click(object sender, EventArgs e)
    {
        if (DialogResult.OK == ofdPhoto.ShowDialog(this.Parent))
        {
            // signalling that the dialog have been used have have
            // useful data
            ofdPhoto.Tag = (object)true;
        }
    }

What I am saving is List:

List<Contact> contactList;

I have only Person in the list

[Serializable]
public class Person : Contact
{
    #region variables and properties

    //public string Photo { get; set; }
    public string Photo;

    private string lastName;
    public string LastName
    {
        get { return lastName; }
        set { lastName = value; base.ComparisonKey = value; }
    }

    public string FirstName { get; set; }
    //  no contructor
}

Edit

In the end of the program I save all data:

  private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
    {
        data.Save();
    }

The file name path that I get with the OpenFileDialog is for a photo.
The path is stripped and only the file name is stored. That part I have checked.
The file name is stored in a Person object, the Person object is stored in List
and in the end of program the whole array is persisted.
End edit
It doesn't depend on the format on the string I am loading

Edit: I have called Dispose on the OpenFileDialog before I save with the BinaryFormatter.Serializer
When I comment out the contents of btnPhoto_Click it BinaryFormatter.Serializer works.

I would appriciate any ideas.

A: 

I didn't saved my file with an absolute path but with just a file name. When OpenFileDialog was used the current directory was changed and the data, the array was stored in current directory that was a totally different place than in the beginning of the program.

And when I restarted the program, the current directory was reset and all the data seemed to be thrown away.
One solution is described in this link
Or just save current directory in a field link

Gorgen