58 lines
1.6 KiB
C#
58 lines
1.6 KiB
C#
using CodeContextGenerator.Interfaces;
|
|
using System.IO;
|
|
using System.Text.Json;
|
|
|
|
namespace CodeContextGenerator.Services;
|
|
|
|
public class SettingsService : ISettingsService
|
|
{
|
|
private const string SettingsFileName = "app_settings.json";
|
|
private readonly string _settingsFilePath;
|
|
|
|
public SettingsService()
|
|
{
|
|
string appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
|
|
string appFolder = Path.Combine(appDataPath, "CodeContextGenerator");
|
|
Directory.CreateDirectory(appFolder);
|
|
_settingsFilePath = Path.Combine(appFolder, SettingsFileName);
|
|
}
|
|
|
|
public string GetLastProjectPath()
|
|
{
|
|
try
|
|
{
|
|
if (File.Exists(_settingsFilePath))
|
|
{
|
|
var json = File.ReadAllText(_settingsFilePath);
|
|
var settings = JsonSerializer.Deserialize<Settings>(json);
|
|
return settings?.LastProjectPath;
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
// Игнорируем ошибки чтения настроек
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public void SaveLastProjectPath(string path)
|
|
{
|
|
try
|
|
{
|
|
var settings = new Settings { LastProjectPath = path };
|
|
var options = new JsonSerializerOptions { WriteIndented = true };
|
|
string json = JsonSerializer.Serialize(settings, options);
|
|
File.WriteAllText(_settingsFilePath, json);
|
|
}
|
|
catch
|
|
{
|
|
// Игнорируем ошибки сохранения настроек
|
|
}
|
|
}
|
|
|
|
private class Settings
|
|
{
|
|
public string LastProjectPath { get; set; }
|
|
}
|
|
}
|