/// <summary> /// カレンダーのインポート処理 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ImportButton_Click(object sender, EventArgs e) { try { string kana = "カレンダー"; string file1 = "mcrc"; string file2 = "MCRC"; var dialog = new CommonOpenFileDialog(kana + "ファイルの選択"); // ファイル選択モード dialog.IsFolderPicker = false; dialog.Multiselect = false; dialog.Filters.Add(new CommonFileDialogFilter(file2 + "ファイル(*." + file1 + ")", "*." + file1)); if (dialog.ShowDialog(this.Handle) == CommonFileDialogResult.Ok) { CalendarModel model = null; //新取込 using (StreamReader sr = new StreamReader(dialog.FileName, Encoding.Default)) { string jsonString = sr.ReadToEnd(); try { model = JsonConvert.DeserializeObject <CalendarModel>(jsonString); } catch (Exception) {} } if (model == null) { using (FileStream fs = new FileStream(dialog.FileName, FileMode.Open, FileAccess.Read)) { BinaryFormatter f = new BinaryFormatter(); string jsonString = (string)f.Deserialize(fs); jsonString = CryptUtil.DecryptString(jsonString, StringValue.CRYPT_PASSWORD); model = JsonConvert.DeserializeObject <CalendarModel>(jsonString); } } //IDを更新する model.CalendarId = CalendarInfos.GetNewCalendarModel().CalendarId; DialogResult result = this.ShowInfoDialog("保存確認", kana + "をインポートしますか?", MessageBoxButtons.YesNo, MessageBoxDefaultButton.Button1); if (result == DialogResult.No) { return; } CalendarInfos.GetInstance().CreateCalendarValue(model.CalendarId, model.Description, model, model.CalendarType); LoadCalendarData(); } } catch (Exception ex) { throw Program.ThrowException(ex); } }
/// <summary> /// ファイルからプロジェクト、又はモジュールをインポートします。 /// </summary> /// <param name="type"></param> private void ImportFromFile(ExecDataType type) { try { string kana = "プロジェクト"; string file1 = "mcrp"; string file2 = "MCRP"; switch (type) { case ExecDataType.MACRO: kana = "モジュール"; file1 = "mcrm"; file2 = "MCRM"; break; } var dialog = new CommonOpenFileDialog(kana + "ファイルの選択"); // ファイル選択モード dialog.IsFolderPicker = false; dialog.Multiselect = false; dialog.Filters.Add(new CommonFileDialogFilter(file2 + "ファイル(*." + file1 + ")", "*." + file1)); if (dialog.ShowDialog(this.Handle) == CommonFileDialogResult.Ok) { ProjectModel model = null; //平文フォーマット using (StreamReader sr = new StreamReader(dialog.FileName, Encoding.Default)) { try { string jsonString = sr.ReadToEnd(); model = JsonConvert.DeserializeObject <ProjectModel>(jsonString); model = LoadProjectForm.RebuildProject(model); } catch (Exception) { } } //新フォーマット if (model == null) { using (FileStream fs = new FileStream(dialog.FileName, FileMode.Open, FileAccess.Read)) { try { BinaryFormatter f = new BinaryFormatter(); model = (ProjectModel)f.Deserialize(fs); model = LoadProjectForm.RebuildProject(model); } catch (Exception) { } } } //旧フォーマット if (model == null) { using (FileStream fs = new FileStream(dialog.FileName, FileMode.Open, FileAccess.Read)) { BinaryFormatter f = new BinaryFormatter(); string jsonString = (string)f.Deserialize(fs); jsonString = CryptUtil.DecryptString(jsonString, StringValue.CRYPT_PASSWORD); model = LoadProjectForm.GetProjectFromJsonString(jsonString); } } switch (type) { case ExecDataType.PROJECT: if (model.ExecDataType != ExecDataType.PROJECT) { throw new Exception(kana + "ファイルではありません。"); } break; case ExecDataType.MACRO: if (model.ExecDataType != ExecDataType.MACRO) { throw new Exception(kana + "ファイルではありません。"); } break; } model.Name = DateTime.Now.ToString("yyyyMMddHHmmss_") + model.Name; //IDを更新する model.RenewId(); DialogResult result = this.ShowInfoDialog("保存確認", kana + "をインポートしますか?", MessageBoxButtons.YesNo, MessageBoxDefaultButton.Button1); if (result == DialogResult.No) { return; } ProcessEditForm.SaveProject(model); this.ShowDialog("保存成功", kana + "を保存しました。"); switch (type) { case ExecDataType.PROJECT: ShowEditProject(this, 0, MacroStartType.EDITORSTART); break; case ExecDataType.MACRO: ShowEditProject(this, 1, MacroStartType.EDITORSTART); break; } } } catch (Exception ex) { throw Program.ThrowException(ex); } }