public static string Save(string filename, Settings settings) { string msg = string.Empty; try { using (MemoryStream ms = new MemoryStream()) { XmlSerializer xs = new XmlSerializer(typeof(Settings)); xs.Serialize(ms, settings); using (FileStream fs = new FileStream(filename, File.Exists(filename) ? FileMode.Truncate : FileMode.Create)) { ms.WriteTo(fs); fs.Close(); } ms.Close(); } } catch(Exception ex) { msg = ex.Message; } return msg; }
/// <summary> /// Load settings from user.settings file /// </summary> private void LoadSettings() { try { settings = Settings.Load(getSettingFile()); } catch (Exception ex) { SetStatus(ex.Message); settings = new Settings(); } portName = settings.PortName; baud = settings.Baud; protocol = settings.Protocol; folder = settings.Folder; if (settings.Commands != null) foreach (CmdButton cmd in CmdButtons(Controls)) { string text = settings.GetCommand(cmd.Index); cmd.Text = text; cmd.Command = text; cmd.Template = string.Empty; toolTip.SetToolTip(cmd, null); } foreach (CommandEx cmd in settings.CommandsEx) { cmd.Name = (cmd.Name ?? string.Empty).Trim(); string text = cmd.Text = (cmd.Text ?? string.Empty).Trim(); if (!string.IsNullOrEmpty(cmd.Name) && !string.IsNullOrEmpty(text)) { CmdButton cb = FindCmdButton(Controls, cmd.Name); if (cb != null) { cb.Text = text; cb.Command = cmd.Command; if (string.IsNullOrEmpty(cb.Command)) cb.Command = text; cb.Template = cmd.Template; toolTip.SetToolTip(cb, cmd.ToolTip); } } } foreach (CmdButton cmd in CmdButtons(Controls)) SetTextCommand(cmd); DTR.Checked = settings.DTR; RTS.Checked = settings.RTS; CRorCRLF.Checked = settings.CRorCRLF; Echo.Checked = settings.Echo; WordWrap.Checked = settings.WordWrap; AutoLSCRIPT.Checked = settings.AutoLSCRIPT; editor.Text = settings.Editor; compiler.Text = settings.Compiler; if (settings.Width > 0 && settings.Height > 0 && (Width != settings.Width || Height != settings.Height) ) { Width = settings.Width; Height = settings.Height; } }
public static Settings Load(string filename) { Settings settings = null; if (File.Exists(filename)) { using (FileStream fs = new FileStream(filename, FileMode.Open)) { try { XmlSerializer xs = new XmlSerializer(typeof(Settings)); settings = xs.Deserialize(fs) as Settings; } catch (Exception ex) { throw ex; } fs.Close(); } } if (settings == null) settings = new Settings(); return settings; }