41 lines
1.3 KiB
C#
41 lines
1.3 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.ObjectModel;
|
|
using System.IO;
|
|
using System.Runtime.Serialization.Formatters.Binary;
|
|
using Päevaküsimus.Models;
|
|
|
|
namespace Päevaküsimus
|
|
{
|
|
public class SerializationService
|
|
{
|
|
static private string _fileName = "data.bin";
|
|
|
|
public static void Serialize(object o)
|
|
{
|
|
using (Stream stream = new FileStream(_fileName, FileMode.Create, FileAccess.Write, FileShare.Write))
|
|
{
|
|
BinaryFormatter binaryFormatter = new BinaryFormatter();
|
|
binaryFormatter.Serialize(stream, o);
|
|
}
|
|
}
|
|
|
|
public static ObservableCollection<Question> DeSerialize ()
|
|
{
|
|
try
|
|
{
|
|
using (Stream stream = new FileStream(_fileName, FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read))
|
|
{
|
|
BinaryFormatter binaryFormatter = new BinaryFormatter();
|
|
ObservableCollection<Question> questions = binaryFormatter.Deserialize(stream) as ObservableCollection<Question>;
|
|
return questions;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
System.Diagnostics.Trace.WriteLine("Error: " + ex.Message);
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
} |