Запоминание предыдущего файла
This commit is contained in:
74
Sources/WindowParameters.cs
Normal file
74
Sources/WindowParameters.cs
Normal file
@@ -0,0 +1,74 @@
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace GtaVUsersInfo.Sources
|
||||
{
|
||||
[Serializable]
|
||||
public class WindowParameters
|
||||
{
|
||||
public int width;
|
||||
public int height;
|
||||
public int leftPos;
|
||||
public int rightPos;
|
||||
public string? prevFile;
|
||||
|
||||
public WindowParameters() { }
|
||||
|
||||
public WindowParameters(Size window, Size pos)
|
||||
{
|
||||
width = window.Width;
|
||||
height = window.Height;
|
||||
leftPos = pos.Width;
|
||||
rightPos = pos.Height;
|
||||
}
|
||||
|
||||
public WindowParameters(Size window, Size pos, string file)
|
||||
{
|
||||
width = window.Width;
|
||||
height = window.Height;
|
||||
leftPos = pos.Width;
|
||||
rightPos = pos.Height;
|
||||
prevFile = file;
|
||||
}
|
||||
|
||||
public static void SaveWindowParameters(WindowParameters fontSettings)
|
||||
{
|
||||
string json = JsonConvert.SerializeObject(fontSettings, Formatting.Indented);
|
||||
|
||||
if (!Directory.Exists(Path.GetDirectoryName(Resources.windowParametersJsonPath)))
|
||||
{
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(Resources.windowParametersJsonPath));
|
||||
}
|
||||
|
||||
File.WriteAllText(Resources.windowParametersJsonPath, json);
|
||||
}
|
||||
|
||||
public static WindowParameters LoadWindowParameters()
|
||||
{
|
||||
try
|
||||
{
|
||||
string json = File.ReadAllText(Resources.windowParametersJsonPath);
|
||||
return JsonConvert.DeserializeObject<WindowParameters>(json);
|
||||
}
|
||||
catch (FileNotFoundException)
|
||||
{
|
||||
Console.WriteLine($"Файл с параметрами окна не найден: {Resources.windowParametersJsonPath}");
|
||||
return null;
|
||||
}
|
||||
catch (JsonException ex)
|
||||
{
|
||||
Console.WriteLine($"Ошибка при десериализации JSON параметрами окна: {ex.Message}");
|
||||
return null;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Произошла неожиданная ошибка при открытии файла с параметрами окна: {ex.Message}");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user