mirror of
https://github.com/jie65535/ChineseChess.git
synced 2024-07-27 18:55:00 +08:00
44 lines
1.2 KiB
C#
44 lines
1.2 KiB
C#
using System.IO;
|
|
using System.Text;
|
|
|
|
using ChineseChess.GUI.Core.Contracts.Services;
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
namespace ChineseChess.GUI.Core.Services
|
|
{
|
|
public class FileService : IFileService
|
|
{
|
|
public T Read<T>(string folderPath, string fileName)
|
|
{
|
|
var path = Path.Combine(folderPath, fileName);
|
|
if (File.Exists(path))
|
|
{
|
|
var json = File.ReadAllText(path);
|
|
return JsonConvert.DeserializeObject<T>(json);
|
|
}
|
|
|
|
return default;
|
|
}
|
|
|
|
public void Save<T>(string folderPath, string fileName, T content)
|
|
{
|
|
if (!Directory.Exists(folderPath))
|
|
{
|
|
Directory.CreateDirectory(folderPath);
|
|
}
|
|
|
|
var fileContent = JsonConvert.SerializeObject(content);
|
|
File.WriteAllText(Path.Combine(folderPath, fileName), fileContent, Encoding.UTF8);
|
|
}
|
|
|
|
public void Delete(string folderPath, string fileName)
|
|
{
|
|
if (fileName != null && File.Exists(Path.Combine(folderPath, fileName)))
|
|
{
|
|
File.Delete(Path.Combine(folderPath, fileName));
|
|
}
|
|
}
|
|
}
|
|
}
|