private void add_Click(object sender, EventArgs e) { try { string path = MainFormModel.CombinePathsStatic(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "\\AirPodsUI"); string file = MainFormModel.CombinePathsStatic(path, "PairedDevices.json"); string contents = ""; using (StreamReader sr = new StreamReader(file)) { string line; while ((line = sr.ReadLine()) != null) { contents += line; } } PairedDevices pdevices = new PairedDevices(); pdevices = PairedDevices.FromJson(contents); pdevices.Devices.Add(new Device() { DeviceAddress = devices[usbDevices.SelectedIndex].DeviceID, DeviceName = name.Text, DeviceType = "USB", TemplateLocation = "" }); string result = Serialize.ToJson(pdevices); using (StreamWriter sw = new StreamWriter(file)) { sw.WriteLine(result); } this.Close(); } catch (Exception ee) { MessageBox.Show("Unable to add to list", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public void RemoveDeviceAt(int index) { devices.Devices.RemoveAt(index); string result = Serialize.ToJson(devices); using (StreamWriter sw = new StreamWriter(CombinePaths(appDocs, "\\PairedDevices.json"))) { sw.WriteLine(result); } string newResult = ""; using (StreamReader sr = new StreamReader(CombinePaths(appDocs, "\\PairedDevices.json"))) { string line = ""; while ((line = sr.ReadLine()) != null) { newResult += line; } } devices = PairedDevices.FromJson(newResult); }
public void RefreshDevices() { try { string json = String.Empty; using (StreamReader sr = new StreamReader(CombinePaths(appDocs, "PairedDevices.json"))) { string line = ""; while ((line = sr.ReadLine()) != null) { json += line; } } devices = PairedDevices.FromJson(json); } catch (Exception e) { if (File.Exists(CombinePaths(appDocs, "PairedDevices.json"))) { File.Delete(CombinePaths(appDocs, "PairedDevices.json")); } File.WriteAllText(CombinePaths(appDocs, "PairedDevices.json"), devices.ToJson()); } }
public static string ToJson(this PairedDevices self) => JsonConvert.SerializeObject(self, ConfigutatorUI.Converter.Settings);
public MainFormModel() { devices = new PairedDevices(); templates = new List <Template>(); openedTemplate = 0; appDocs = CombinePaths(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "\\AirPodsUI"); TemplateLocations = new List <string>(); #region Directory Creation if (!Directory.Exists(appDocs)) { Directory.CreateDirectory(appDocs); } if (!Directory.Exists(CombinePaths(appDocs, "Logs"))) { Directory.CreateDirectory(CombinePaths(appDocs, "Logs")); } if (!Directory.Exists(CombinePaths(appDocs, "Assets"))) { Directory.CreateDirectory(CombinePaths(appDocs, "Assets")); } if (!Directory.Exists(CombinePaths(appDocs, "Templates"))) { Directory.CreateDirectory(CombinePaths(appDocs, "Templates")); } #endregion foreach (var i in Directory.GetFiles(CombinePaths(appDocs, "Templates"))) { if (Path.GetExtension(i.ToLower()) == ".json") { try { templates.Add(Template.FromJson(File.ReadAllText(i))); TemplateLocations.Add(i); } catch (Exception e) { continue; } } } try { string json = String.Empty; using (StreamReader sr = new StreamReader(CombinePaths(appDocs, "PairedDevices.json"))) { string line = ""; while ((line = sr.ReadLine()) != null) { json += line; } } devices = PairedDevices.FromJson(json); } catch (Exception e) { if (File.Exists(CombinePaths(appDocs, "PairedDevices.json"))) { File.Delete(CombinePaths(appDocs, "PairedDevices.json")); } File.WriteAllText(CombinePaths(appDocs, "PairedDevices.json"), devices.ToJson()); } }