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.