用ConfigurationSection自定义配置文件
配置文件:
1 <?xml version="1.0" encoding="utf-8" ?> 2 <configuration> 3 <configSections> 4 <section name="MonitorSection" type="WatchDog.MonitorSection,WatchDog"/> 5 <MonitorSection> 6 <FTPFiles Interval="1"> 7 <!--CheckType分两种类型:0:定点检查(FixedPoint)、1:循环检查(Cycle)--> 8 <FTPFileDesc Alias="FTP File 1" FilePath="/reports/pricing.csv.zip" LocalPath="E:\Test\" FtpDomain ="192168.1.111" UserName="user1" Password="123456" CheckType="0" CheckPoint="0830" /> 9 <FTPFileDesc Alias="FTP File 2" FilePath="/reports/basic_{yyyy}{mm}{dd}.csv.zip" LocalPath="E:\Test\" FtpDomain ="192.168.2.222" UserName="user2" Password="123456" CheckType="1" Interval="30" /> 10 </FTPFiles> 11 <DiskFiles Interval="1"> 12 <DiskFileDesc Alias="Local File 1" FilePath="E:\Test\FTP\equity_prices.zip" CheckType="0" CheckPoint="0830" /> 13 <DiskFileDesc Alias="Local File 2" FilePath="E:\Test\FTP\equity_orders.zip" CheckType="1" Interval="30"/> 14 </DiskFiles> 15 </MonitorSection> 16 </configuration>
using System; using System.Configuration; namespace WatchDog { public enum CheckTypeEnum { FixedPoint = 0, Cycle = 1 } public class MonitorSection : ConfigurationSection { [ConfigurationProperty("FTPFiles", IsDefaultCollection = true)] public FTPFileCollection FtpFileList { get { return (FTPFileCollection)this["FTPFiles"]; } } [ConfigurationProperty("DiskFiles", IsDefaultCollection = true)] public DiskFileCollection DiskFileList { get { return (DiskFileCollection)this["DiskFiles"]; } } } public class BaseFileDesc : ConfigurationElement { [ConfigurationProperty("FilePath", IsRequired = true)] public string FilePath { get { return (string)this["FilePath"]; } set { this["FilePath"] = value; } } [ConfigurationProperty("Alias", IsRequired = true)] public string Alias { get { return (string)this["Alias"]; } set { this["Alias"] = value; } } [ConfigurationProperty("CheckType", IsRequired = true)] public int CheckType { get { return (int)this["CheckType"]; } set { this["CheckType"] = value; } } [ConfigurationProperty("Interval", IsRequired = false, DefaultValue = 5)] public int Interval { get { return (int)this["Interval"]; } set { this["Interval"] = value; } } [ConfigurationProperty("CheckPoint", IsRequired = false,DefaultValue = 0830)] public int CheckPoint { get { return (int)this["CheckPoint"]; } set { this["CheckPoint"] = value; } } public bool CheckStatus { get; set; } public string CheckMessage { get; set; } public long FileSize { get; set; } public bool MailSendFlag { get; set; } public DateTime CurrCheckPoint{ get { if (!_setCurrPointFlag) { _currentCheckPoint = DateTime.Today.AddHours(CheckPoint / 100).AddMinutes(CheckPoint % 100); } return _currentCheckPoint; } set { _currentCheckPoint = value; _setCurrPointFlag = true; } } private bool _setCurrPointFlag = false; private DateTime _currentCheckPoint; } public class FTPFileDesc : BaseFileDesc { [ConfigurationProperty("LocalPath", IsRequired = true)] public string LocalPath { get { return (string)this["LocalPath"]; } set { this["LocalPath"] = value; } } [ConfigurationProperty("FtpDomain", IsRequired = true)] public string FtpDomain { get { return (string)this["FtpDomain"]; } set { this["FtpDomain"] = value; } } [ConfigurationProperty("UserName", IsRequired = false)] public string UserName { get { return (string)this["UserName"]; } set { this["UserName"] = value; } } [ConfigurationProperty("Password", IsRequired = false)] public string Password { get { return (string)this["Password"]; } set { this["Password"] = value; } } } public class DiskFileDesc : BaseFileDesc { } public abstract class BaseFileCollection : ConfigurationElementCollection { protected override ConfigurationElement CreateNewElement() { return new BaseFileDesc(); } public override ConfigurationElementCollectionType CollectionType { get { return ConfigurationElementCollectionType.BasicMap; } } public BaseFileDesc this[int index] { get { return (BaseFileDesc)BaseGet(index); } set { if (BaseGet(index) != null) { BaseRemoveAt(index); } BaseAdd(index, value); } } [ConfigurationProperty("Interval", IsRequired = false, DefaultValue = 5)] public int Interval { get { return (int)this["Interval"]; } set { this["Interval"] = value; } } } public class DiskFileCollection : BaseFileCollection { protected override ConfigurationElement CreateNewElement() { return new DiskFileDesc(); } protected override object GetElementKey(ConfigurationElement element) { return ((DiskFileDesc)element).FilePath; } protected override string ElementName { get { return "DiskFileDesc"; } } public DiskFileDesc this[int index] { get { return (DiskFileDesc)BaseGet(index); } set { if (BaseGet(index) != null) { BaseRemoveAt(index); } BaseAdd(index, value); } } } public class FTPFileCollection : BaseFileCollection { protected override ConfigurationElement CreateNewElement() { return new FTPFileDesc(); } protected override object GetElementKey(ConfigurationElement element) { return ((FTPFileDesc)element).FtpDomain + ((FTPFileDesc)element).FilePath; } protected override string ElementName { get { return "FTPFileDesc"; } } public FTPFileDesc this[int index] { get { return (FTPFileDesc)BaseGet(index); } set { if (BaseGet(index) != null) { BaseRemoveAt(index); } BaseAdd(index, value); } } } }
posted on 2015-01-27 17:56 Donald_2010 阅读(136) 评论(0) 收藏 举报
浙公网安备 33010602011771号