private void tmiLoadJsonDirectory_Click(object sender, EventArgs e) { #if DEBUG fbdMain.SelectedPath = @"C:\Programs\WinForm\JsonEditor\JsonEditor\TestData"; #endif DialogResult dr = fbdMain.ShowDialog(this); if (dr == DialogResult.OK) { tmiCloseAllJsonFiles_Click(this, e); jfi = new JFilesInfo(fbdMain.SelectedPath.Substring(fbdMain.SelectedPath.LastIndexOf("\\") + 1), fbdMain.SelectedPath); string[] jsonfiles = Directory.GetFiles(jfi.DirectoryPath, "*.json"); tables = new Dictionary <string, JTable>(); foreach (string file in jsonfiles) { using (FileStream fs = new FileStream(file, FileMode.Open)) { StreamReader sr = new StreamReader(fs); if (file == Path.Combine(jfi.DirectoryPath, linkFileName)) { jfi = JsonConvert.DeserializeObject <JFilesInfo>(sr.ReadToEnd()); jfi.DirectoryPath = fbdMain.SelectedPath; } else { tables.Add(Path.GetFileNameWithoutExtension(file), new JTable(Path.GetFileNameWithoutExtension(file), JsonConvert.DeserializeObject(sr.ReadToEnd()))); } sr.Close(); } } //有JFileInfo的話相連 try { if (jfi.FilesInfo.Count != 0) { foreach (JTable jt in tables.Values) { jt.LoadFileInfo(jfi.FilesInfo.Find(m => m.Name == jt.Name)); } } } catch (Exception ex) { MessageBox.Show($"JFilesInfo連結失敗,忽略後繼續編輯:{ex.Message}", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); } RefreshJsonFilesUI(); sslMain.Text = $"{tables.Count} 檔案已讀入"; } }
private void tmiNewJsonFiles_Click(object sender, EventArgs e) { #if DEBUG fbdMain.SelectedPath = @"C:\Programs\WinForm\JsonEditor\JsonEditor\TestData"; #endif DialogResult dr = fbdMain.ShowDialog(this); if (dr == DialogResult.OK) { jfi = new JFilesInfo(fbdMain.SelectedPath.Substring(fbdMain.SelectedPath.LastIndexOf("\\") + 1), fbdMain.SelectedPath); string[] jsonfiles = Directory.GetFiles(fbdMain.SelectedPath, "*.json"); if (jsonfiles.Length > 0) { if (jsonfiles.Length == 1 && jsonfiles[0] == Path.Combine(fbdMain.SelectedPath, linkFileName)) { File.Delete(jsonfiles[0]); } else { if (File.Exists(Path.Combine(fbdMain.SelectedPath, linkFileName))) { dr = MessageBox.Show($"此文件已有現存{jsonfiles.Length - 1} JSON檔案,是否要清空資料夾", "清空Json檔案", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); } else { dr = MessageBox.Show($"此文件已有現存{jsonfiles.Length} JSON檔案,是否要清空資料夾", "清空Json檔案", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); } if (dr == DialogResult.Yes) { foreach (string s in jsonfiles) { File.Delete(s); } } else { return; } } } tmiCloseAllJsonFiles_Click(this, e); tables = new Dictionary <string, JTable>(); jfi.DirectoryPath = fbdMain.SelectedPath; tmiCloseAllJsonFiles.Enabled = true; RefreshJsonFilesUI(); sslMain.Text = $"已在\"{jfi.DirectoryPath}\"新建檔案資料夾"; } }