WIn_RustTools/RustTools/Services/FileService.cs

42 lines
1.0 KiB
C#
Raw Normal View History

2024-07-14 11:24:10 +00:00
using System.Text;
using Newtonsoft.Json;
2024-07-29 01:10:42 +00:00
using RustTools.Contracts.Services;
2024-07-14 11:24:10 +00:00
2024-07-29 01:10:42 +00:00
namespace RustTools.Services;
2024-07-14 11:24:10 +00:00
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));
}
}
}