/// <summary> /// プリセット情報の削除 /// </summary> /// <param name="id"></param> public async Task DeletePresetAsync(string id) { IsBusy = true; using (new OnDisposeAction(() => IsBusy = false)) { var preset = UserSettings.PresetInfos.Where(e => e.Id == id).FirstOrDefault(); if (preset == null) { return; } var path = Path.Combine(UserSettingsService.AppSettings.AppDataPath, preset.FileName); try { FileService.DeleteFile(path); UserSettings.PresetInfos.Remove(preset); await UserSettingsService.SaveAsync(); if (CurrentPreset.Id == preset.Id) { CurrentPreset = null; } } catch (IOException e) { Trace.TraceInformation($"プリセット[{preset.Id}]削除失敗。{path}:{e.Message}"); ErrorOccurred?.Invoke(this, new ConfigJsonErrorEventArgs(ConfigJsonErrorEventArgs.Cause.DeletePresetError, e, path)); } } }
/// <summary> /// 新規のプリセット情報として保存 /// </summary> /// <param name="presetName"></param> /// <returns></returns> public async Task SaveNewPresetAsync(string presetName) { IsBusy = true; using (new OnDisposeAction(() => IsBusy = false)) { var id = Guid.NewGuid().ToString("D"); var fileName = $"{PresetNameToFileNameRegex.Replace(presetName, "")}_{id}.json"; var filePath = Path.Combine(UserSettingsService.AppSettings.AppDataPath, fileName); try { await FileService.WriteAsync(filePath, ConfigJson, UserSettings.MergeUnknownJsonProperty); var preset = new PresetInfo() { Id = id, Name = presetName, FileName = fileName, }; UserSettings.PresetInfos.Add(preset); CurrentPreset = preset; await UserSettingsService.SaveAsync(); ConfigJson.IsChanged = false; } catch (Exception e) when(e is IOException || e is JsonException) { Trace.TraceInformation($"プリセット{presetName}作成失敗。{filePath}:{e.Message}"); ErrorOccurred?.Invoke(this, new ConfigJsonErrorEventArgs(ConfigJsonErrorEventArgs.Cause.CreatePresetError, e, filePath)); } } }