/
KDP
/
SuprTimeLogAutomation
Обзор
Документация
Войти
/
KDP
/
SuprTimeLogAutomation
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
Services/ConfigService.cs
54 строки
2 KB
Timshin Mikhail
first commit
24 июн 2026, 14:52
24 июн 2026, 14:52
61ceede
Код
Авторство
О чём код?
using System.Text.Json; using SuprTimeLogAutomation.Models; namespace SuprTimeLogAutomation.Services; public class ConfigService { private const string ConfigFileName = "config.json"; public static async Task<AppConfig> LoadAsync() { var exeDir = Path.GetDirectoryName(Environment.ProcessPath) ?? AppContext.BaseDirectory; var configPath = Path.Combine(exeDir, "config.json"); Console.WriteLine($" Папка запуска: {exeDir}"); Console.WriteLine($"📄 Ищем конфиг: {configPath}"); if (!File.Exists(configPath)) throw new FileNotFoundException($"config.json не найден рядом с программой.\nПоложите файл config.json в ту же папку, где лежит .exe"); var json = await File.ReadAllTextAsync(configPath); var config = JsonSerializer.Deserialize<AppConfig>(json, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); if (config?.Supr == null || string.IsNullOrWhiteSpace(config.Supr.Username) || string.IsNullOrWhiteSpace(config.Supr.Password)) throw new InvalidOperationException("В config.json не заполнены Supr:Username или Supr:Password"); return config; // var path = configPath ?? Path.Combine(AppContext.BaseDirectory, ConfigFileName); // // if (!File.Exists(path)) // throw new FileNotFoundException($"Файл конфигурации не найден: {path}"); // // var json = await File.ReadAllTextAsync(path); // var config = JsonSerializer.Deserialize<AppConfig>(json, new JsonSerializerOptions // { // PropertyNameCaseInsensitive = true, // ReadCommentHandling = JsonCommentHandling.Skip // }); // // Validate(config); // return config; } private static void Validate(AppConfig config) { if (string.IsNullOrWhiteSpace(config.Supr?.BaseUrl)) throw new ArgumentException("Supr:BaseUrl не задан"); if (string.IsNullOrWhiteSpace(config.TimeLog?.ProgramPath)) throw new ArgumentException("TimeLog:ProgramPath не задан"); if (config.ProjectMapping == null || config.ProjectMapping.Count == 0) throw new ArgumentException("ProjectMapping пуст"); } }