mirror of
https://github.com/ultrasn0w/app.git
synced 2025-12-13 12:19:54 +01:00
78 lines
1.7 KiB
C#
78 lines
1.7 KiB
C#
using System.Text.Json;
|
|
using APP.DTO;
|
|
|
|
namespace APP.Utility;
|
|
|
|
internal static class SaveBoy
|
|
{
|
|
private static readonly string AuthFile = ".." + Path.DirectorySeparatorChar + "reg.json";
|
|
|
|
internal static async Task<Auth?> ReadAuth()
|
|
{
|
|
if (File.Exists(AuthFile))
|
|
{
|
|
return await ReadFromFile<Auth>(AuthFile);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
internal static void DeleteAuth()
|
|
{
|
|
if (File.Exists(AuthFile))
|
|
{
|
|
File.Delete(AuthFile);
|
|
}
|
|
}
|
|
|
|
internal static async Task SaveAuth(Auth auth)
|
|
{
|
|
await WriteToFile(AuthFile, auth);
|
|
}
|
|
|
|
private static async Task WriteToFile<T>(string filePath, T objectToWrite)
|
|
{
|
|
try
|
|
{
|
|
await using var sw = new StreamWriter(filePath, false);
|
|
try
|
|
{
|
|
var res = JsonSerializer.Serialize(objectToWrite);
|
|
await sw.WriteAsync(res);
|
|
}
|
|
finally
|
|
{
|
|
sw.Close();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
await Console.Error.WriteLineAsync(ex.Message);
|
|
}
|
|
}
|
|
|
|
private static async Task<T?> ReadFromFile<T>(string filePath)
|
|
{
|
|
try
|
|
{
|
|
using var sr = new StreamReader(filePath);
|
|
try
|
|
{
|
|
var text = await sr.ReadToEndAsync();
|
|
var res = JsonSerializer.Deserialize<T>(text);
|
|
return res;
|
|
}
|
|
finally
|
|
{
|
|
sr.Close();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
await Console.Error.WriteLineAsync(ex.Message);
|
|
}
|
|
|
|
return default;
|
|
}
|
|
}
|