62 lines
1.6 KiB
C#
62 lines
1.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.IO;
|
|
using System.Xml;
|
|
using System.Xml.Serialization;
|
|
using ZipArchiver.Application;
|
|
|
|
namespace ZipArchiver.Database
|
|
{
|
|
public class FileDatabase : IDatabase
|
|
{
|
|
private string fileName;
|
|
public FileDatabase(string fileName)
|
|
{
|
|
this.fileName = fileName;
|
|
|
|
if (!File.Exists(fileName))
|
|
{
|
|
Write(default(ZipperConfig));
|
|
}
|
|
}
|
|
|
|
public ZipperConfig Read()
|
|
{
|
|
if (!File.Exists(fileName))
|
|
{
|
|
return default(ZipperConfig);
|
|
}
|
|
|
|
try
|
|
{
|
|
XmlSerializer serializer = new XmlSerializer(typeof(ZipperConfig));
|
|
using (StreamReader reader = new StreamReader(fileName, new UTF8Encoding(false)))
|
|
{
|
|
return (ZipperConfig)serializer.Deserialize(reader);
|
|
}
|
|
} catch (IOException e)
|
|
{
|
|
Console.Write(e);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public void Write(ZipperConfig config)
|
|
{
|
|
try
|
|
{
|
|
XmlSerializer serializer = new XmlSerializer(typeof(ZipperConfig));
|
|
using (StreamWriter writer = new StreamWriter(fileName, false, new UTF8Encoding(false)))
|
|
{
|
|
serializer.Serialize(writer, config);
|
|
}
|
|
} catch (IOException)
|
|
{
|
|
}
|
|
}
|
|
}
|
|
}
|