You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.9 KiB
60 lines
1.9 KiB
using Model;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Runtime.Serialization;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Xml;
|
|
|
|
namespace Persistance
|
|
{
|
|
public class DataSerializerBinary
|
|
{
|
|
public static void Serializer(string path, Theque theque)
|
|
{
|
|
Directory.SetCurrentDirectory(Path.Combine(Directory.GetCurrentDirectory(), path));
|
|
string txtFile = "theque.txt";
|
|
|
|
var serializer = new DataContractSerializer(typeof(Theque));
|
|
|
|
using (FileStream stream = File.Create(txtFile))
|
|
{
|
|
using (XmlDictionaryWriter xmlDicoWriter = XmlDictionaryWriter.CreateBinaryWriter(stream))
|
|
{
|
|
serializer.WriteObject(xmlDicoWriter, theque);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static Theque Deserializer(string path)
|
|
{
|
|
Directory.SetCurrentDirectory(Path.Combine(Directory.GetCurrentDirectory(), path));
|
|
string txtFile = "theque.txt";
|
|
|
|
var serializer = new DataContractSerializer(typeof(Theque));
|
|
|
|
Theque? theque = new Theque();
|
|
if (File.Exists(txtFile))
|
|
{
|
|
using (FileStream stream = File.OpenRead(txtFile))
|
|
{
|
|
using (XmlDictionaryReader xmlDicoReader = XmlDictionaryReader.CreateBinaryReader(stream, XmlDictionaryReaderQuotas.Max))
|
|
{
|
|
Theque? thequeOpt = serializer.ReadObject(xmlDicoReader) as Theque;
|
|
if (thequeOpt != null)
|
|
theque = thequeOpt;
|
|
else
|
|
Console.WriteLine("Theque est null");
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
theque = Stub.LoadTheque();
|
|
}
|
|
return theque;
|
|
}
|
|
}
|
|
}
|