private void btnMakeJson_Click(object sender, EventArgs e) { DataJson data = new DataJson((int)nUDSenNr.Value, (int)nUDInstNr.Value, (int)nUDBRet.Value, new List <int> { (int)nUDData.Value }.ToArray()); FolderBrowserDialog folderBrowser = new FolderBrowserDialog(); if (folderBrowser.ShowDialog() == DialogResult.OK) { string filePath = folderBrowser.SelectedPath; filePath += "\\" + tBFileName.Text + ".json"; try { json.MakeJson(filePath, data); } catch (DirectoryNotFoundException ex) { MessageBox.Show(ex.Message); } catch (ArgumentOutOfRangeException ex) { MessageBox.Show(ex.Message); } } }
public void MakeJson(string path, DataJson data) { if (data == null) { throw new ArgumentNullException("Data cant be null"); } Data.Add(data); using (StreamWriter file = File.CreateText(path)) { JsonSerializer serializer = new JsonSerializer(); //serialize object directly into file stream serializer.Serialize(file, data); } }