Sviluppo TradingBot
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace DesktopBot.Models
|
||||
{
|
||||
/// <summary>
|
||||
/// Gestisce la persistenza delle istanze di bot in un file JSON locale.
|
||||
/// Percorso: %AppData%\TradingBot\bots.json
|
||||
/// </summary>
|
||||
public static class BotInstanceStore
|
||||
{
|
||||
private static readonly string StorePath = Path.Combine(
|
||||
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
|
||||
"TradingBot",
|
||||
"bots.json"
|
||||
);
|
||||
|
||||
/// <summary>Carica tutti i bot salvati dal disco. Ritorna lista vuota se il file non esiste.</summary>
|
||||
public static List<BotInstance> Load()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!File.Exists(StorePath))
|
||||
return new List<BotInstance>();
|
||||
|
||||
var json = File.ReadAllText(StorePath, Encoding.UTF8);
|
||||
return JsonConvert.DeserializeObject<List<BotInstance>>(json) ?? new List<BotInstance>();
|
||||
}
|
||||
catch
|
||||
{
|
||||
return new List<BotInstance>();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Salva tutti i bot su disco.</summary>
|
||||
public static void Save(IEnumerable<BotInstance> bots)
|
||||
{
|
||||
try
|
||||
{
|
||||
var dir = Path.GetDirectoryName(StorePath);
|
||||
if (!Directory.Exists(dir))
|
||||
Directory.CreateDirectory(dir);
|
||||
|
||||
var json = JsonConvert.SerializeObject(new List<BotInstance>(bots), Formatting.Indented);
|
||||
File.WriteAllText(StorePath, json, Encoding.UTF8);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Logging silenzioso: non bloccare la UI per errori di I/O
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user