private void c_SaveConfigButton_Click(object sender, EventArgs e) { SaveFileDialog sfd = new SaveFileDialog() { Filter = "XML Files|*.xml", CheckPathExists = true }; if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { XmlSerializer x = new XmlSerializer(typeof(SerializedLayers), new Type[] { typeof(LayerInitialLand), typeof(LayerZoom), typeof(LayerRandomBiome) }); SerializedLayers config = new SerializedLayers(); foreach (Layer l in this.c_LayersListBox.Items) { config.Add(l); } using (StreamWriter writer = new StreamWriter(sfd.FileName)) x.Serialize(writer, config); MessageBox.Show(this, "Save successful.", "Configuration saved.", MessageBoxButtons.OK, MessageBoxIcon.Information); } }
private void c_LoadConfigButton_Click(object sender, EventArgs e) { if (MessageBox.Show( this, "Are you sure you want to load a file? You will lose the current configuration!", "Are you sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) != System.Windows.Forms.DialogResult.Yes) { return; } OpenFileDialog ofd = new OpenFileDialog() { Filter = "XML Files|*.xml", CheckFileExists = true, CheckPathExists = true }; if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { this.c_LayersListBox.Items.Clear(); this.RegenerateImage(); XmlSerializer x = new XmlSerializer(typeof(SerializedLayers), new Type[] { typeof(LayerInitialLand), typeof(LayerZoom), typeof(LayerRandomBiome) }); SerializedLayers config = null; using (StreamReader reader = new StreamReader(ofd.FileName)) config = x.Deserialize(reader) as SerializedLayers; if (config == null) { MessageBox.Show(this, "Unable to load configuration file.", "Configuration invalid.", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } for (int i = 0; i < config.Count; i++) { config[i].SetValues(i == 0 ? null : config[i - 1], this.m_Seed); } foreach (Layer l in config) { this.c_LayersListBox.Items.Add(l); } this.RegenerateImage(); } }