Deserializja binarna - problem z wczytaniem danych z pliku

0

Klasy na których wykonuję serializację :

        [Serializable]
public class TrackList
    {
        public List<TrackItem> trackList { get; set; }
        public string name { get; set; }

        public TrackList()
        {
            name = "new";
            trackList = new List<TrackItem>();
        }
    }
    [Serializable]
    public class TrackItem
    {
        public string path { get; set; }
        public string name { get; set; }
        public bool added;

        public TrackItem(string p)
        {
            path = p;

            name = getFileName(path);

            added = false;
        }

        public string getFileName(string p)
        {
            int cut_filename = p.LastIndexOf(@"\") + 1;

            string name = string.Empty;
            name = p.Substring(cut_filename);           // cutting path
            name = name.Substring(0, name.Length - 4);  // cutting file extension

            return name;
        }
    }

W taki sposób :

        private void buttonLoadList_Click(object sender, EventArgs e)
        {
            mainList = SerializeFromFile();
            UpdateListBox();
        }

        private void buttonSaveList_Click(object sender, EventArgs e)
        {
            SerializeToFile(mainList);
        }

        private void SerializeToFile(TrackList tl)
        {
            SaveFileDialog fd = new SaveFileDialog();

            if (fd.ShowDialog() == DialogResult.OK)
            {
                FileStream fs = new FileStream(fd.FileName, FileMode.Create);
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(fs, tl);
                fs.Close();
            }
        }

        private TrackList SerializeFromFile()
        {
            OpenFileDialog fd = new OpenFileDialog();
            TrackList tl = new TrackList();

            if (fd.ShowDialog() == DialogResult.OK)
            {
                FileStream fs = new FileStream(fd.FileName, FileMode.Open);
                BinaryFormatter bf = new BinaryFormatter();
                tl = (TrackList)bf.Deserialize(fs);
            }

            return tl;
        }

I zapisuję się do pliku poprawnie (badane notanikiem :P) ale problem jest podczas wczytywania. Obiekt mainList jest pusty...

PS. Fajna ta nowa strona.

0

Problem rozwiązany, problem leżał w dalszej przeróbce danych. To co wkleiłem raczej ok.

0

Zerknie ktoś jeszcze na tą metodę.

Czemu wywala 'index was out of range'...

        private void listBoxTrackList_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete && listBoxTrackList.SelectedIndex >= 0 && listBoxTrackList.SelectedItem != null)
            {
                try
                {
                    int index = listBoxTrackList.SelectedIndex;

                    listBoxTrackList.Items.RemoveAt(index);

                    mainList.trackList.RemoveAt(index);

                    labelToolBar.Text = "Item removed from List";
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
        }

nawet jak zakomentuję MessageBox to i tak błąd jest na :

 listBoxTrackList.Items.RemoveAt(index);
0

A ile wynosi wartość index, i ile masz elementów w liście?

0

Debuger pokazuję np. 0x0000005 coś takiego i krok później i tak wyrzuca błąd. Czyli ogólnie indeks jest poprawny. Bo usuwam też z List<> która jest 'wklejona' w listtbox i jest ok - bez błędów.

Obiektów mam różnie od 10 do 1000. Zależy ile utworów wczytam. Na pewno górnej granicy nie przekraczam.

Czytałem trochę i mi to wygląda na jakąś zależność między dodanymi obiektami a samym listboxem...ale ekspertem nie jestem i nie umiem tego wytłumaczyć.

1 użytkowników online, w tym zalogowanych: 0, gości: 1