public static SaveObject Deserialize(String streamUrl) { FileStream stream = null; SaveObject dataO = null; try { if (File.Exists(streamUrl)) { stream = File.Open(streamUrl, FileMode.Open); BinaryFormatter serializer = new BinaryFormatter(); object o = serializer.Deserialize(stream); if(o.GetType() == typeof(SaveObject)) { dataO = (SaveObject)o; } else if (o.GetType() == typeof(DataTable)) { DataTable dt = (DataTable)o; dataO = new SaveObject(dt, 100); } else { MessageBox.Show("Nicht kompatible Datei"); DataTable edt = new DataTable(); edt.Columns.Add("Name"); edt.Columns.Add("URL"); edt.Rows.Add(); dataO = new SaveObject(edt, 100); } } else { DataTable edt = new DataTable(); edt.Columns.Add("Name"); edt.Columns.Add("URL"); edt.Rows.Add(); dataO = new SaveObject(edt, 100); } } catch (Exception e) { MessageBox.Show(e.Message); } finally { if (stream != null && stream.CanRead) { stream.Close(); } } return dataO; }
public static void Serialize(DataTable dt, int vol, String streamUrl) { SaveObject so = new SaveObject(dt, vol); try { if (File.Exists(streamUrl)) { File.Delete(streamUrl); } FileStream fs = File.Create(streamUrl); BinaryFormatter serializer = new BinaryFormatter(); serializer.Serialize(fs, so); } catch (Exception e) { MessageBox.Show(e.Message); } }