用类的序列化方法以xml保存配置文件相当方便:
类定义如下:
using System; using System.Collections.Generic; using System.Xml.Serialization; using System.Text; namespace sngWebBrowser { [Serializable()] [XmlRootAttribute("启动配置参数", IsNullable = false)] public class SConfig { public SConfig() { } [XmlElement("启动标识串")] public string strIDStr; [XmlElement("退出标识串")] public string exitIDStr; [XmlElement("启动消息")] public string strMessage; [XmlElement("退出消息")] public string exitMessage; [XmlElement("小于等于此宽度则以全屏模式启动")] public int DWide ; [XmlElement("小于等于此高度则以全屏模式启动")] public int DHeight ; //小于等于此分辩率(AND关系)则自动选中全屏选项,以取消提示框 } }
以下为上面参数类的使用
string filepath = @"c:\te.xml"; SConfig cf = new SConfig(); XmlSerializer ser = new XmlSerializer(typeof(SConfig)); if (File.Exists(filepath)) { FileStream fs = new FileStream(filepath, FileMode.Open); cf = (SConfig)ser.Deserialize(fs); fs.Close(); } else { FileStream fs = new FileStream(filepath, FileMode.CreateNew); TextWriter tw = new StreamWriter(fs); cf.exitIDStr = "11111"; cf.strIDStr = "22222"; cf.strMessage = "ffff"; cf.exitMessage = "gggg"; ser.Serialize(tw, cf); tw.Close(); fs.Close(); }
以上操作相当方便,但配置文件的格式受到相当的限制,只能以<启动消息>value</启动消息>格式储存。
原创文章,作者:苏葳,如需转载,请注明出处:https://www.swmemo.com/185.html