public static SettingsData GetData(MainSettings settings) { if (settings == null) return null; var ports = settings.DevicePorts.Select(port => new DataPair<string, PortSettings>(port.Key, port.Value)).ToList(); var inputs = settings.DiscretInputKeys.Select(port => new DataPair<string, int>(port.Key, port.Value)).ToList(); var relays = settings.RelayKeys.Select(port => new DataPair<string, int>(port.Key, port.Value)).ToList(); return new SettingsData() { DevicePorts = ports, DiscretInputKeys = inputs, RelayKeys = relays }; }
public static void SaveSettings(MainSettings settings,string settingsPath = "settings.xml") { if (File.Exists(settingsPath)) File.Delete(settingsPath); var data = GetData(settings); var archiveType = typeof(SettingsData); var xmlSerializer = new XmlSerializer(archiveType); var dir = Path.GetDirectoryName(settingsPath); if (!Directory.Exists(dir) && !string.IsNullOrEmpty(dir)) Directory.CreateDirectory(dir); using (var fs = new FileStream(settingsPath, FileMode.Create)) { XmlWriter writer = XmlWriter.Create(fs); xmlSerializer.Serialize(writer, data); } }