private void btnExportJson_Click(object sender, EventArgs e) { //if (dgvResult.SelectedRows.Count == 0) return; OpenFileDialog dialog = new OpenFileDialog() { InitialDirectory = @"Z:\SSR", Filter = "Json文件|*.json", RestoreDirectory = true, FilterIndex = 1, FileName = "gui-config.json" }; if (dialog.ShowDialog() == DialogResult.OK) { JsonManipulator jManip = new JsonManipulator(dialog.FileName); foreach (DataGridViewRow row in dgvResult.Rows) { if (!row.Selected || !row.Visible) { continue; } var remark = row.Cells["Location"].Value.ToString(); SS_GUI_Config config = new SS_GUI_Config() { remarks = remark, remarks_base64 = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(remark)), server = row.Cells["IP"].Value.ToString(), server_port = row.Cells["Port"].Value.ToString(), password = row.Cells["Password"].Value.ToString(), method = row.Cells["Method"].Value.ToString() }; jManip.AddConfig(config); } jManip.WriteJsonToFile(); MessageBox.Show("已成功保存到文件:" + dialog.FileName, "保存成功"); } }
public void AddConfig(SS_GUI_Config config) { JArray configs = _JsonObject["configs"] as JArray; configs.Add(JObject.FromObject(config)); }