using System; using System.IO; using Newtonsoft.Json; using VNCServerList.Models; namespace VNCServerList.Services { public class SettingsService { private readonly string _settingsFilePath; private AppSettings _settings; public SettingsService() { _settingsFilePath = Path.Combine( AppDomain.CurrentDomain.BaseDirectory, "VNCServerList", "settings.json" ); LoadSettings(); } public AppSettings GetSettings() { return _settings; } public void SaveSettings(AppSettings settings) { _settings = settings; // 디버깅용 로그 System.Diagnostics.Debug.WriteLine($"설정 저장 시작: 파일 경로={_settingsFilePath}"); System.Diagnostics.Debug.WriteLine($"저장할 설정: VNCViewerPath={settings.VNCViewerPath}, WebServerPort={settings.WebServerPort}"); // 디렉토리가 없으면 생성 var directory = Path.GetDirectoryName(_settingsFilePath); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); System.Diagnostics.Debug.WriteLine($"디렉토리 생성: {directory}"); } // JSON으로 직렬화하여 저장 var json = JsonConvert.SerializeObject(settings, Formatting.Indented); System.Diagnostics.Debug.WriteLine($"JSON 데이터: {json}"); File.WriteAllText(_settingsFilePath, json); System.Diagnostics.Debug.WriteLine("설정 파일 저장 완료"); } private void LoadSettings() { try { System.Diagnostics.Debug.WriteLine($"설정 로드 시작: 파일 경로={_settingsFilePath}"); if (File.Exists(_settingsFilePath)) { var json = File.ReadAllText(_settingsFilePath); System.Diagnostics.Debug.WriteLine($"읽은 JSON: {json}"); _settings = JsonConvert.DeserializeObject(json); System.Diagnostics.Debug.WriteLine($"로드된 설정: VNCViewerPath={_settings.VNCViewerPath}, WebServerPort={_settings.WebServerPort}"); } else { System.Diagnostics.Debug.WriteLine("설정 파일이 없어서 기본값으로 초기화"); _settings = new AppSettings(); SaveSettings(_settings); } } catch (Exception ex) { // 설정 파일 로드 실패 시 기본값 사용 System.Diagnostics.Debug.WriteLine($"설정 로드 오류: {ex.Message}"); _settings = new AppSettings(); } } public void UpdateVNCViewerPath(string path) { _settings.VNCViewerPath = path; SaveSettings(_settings); } } }