Config类
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
using
System;
using System.Xml;
using System.Collections;
using System.IO;
namespace Rocky
{
public sealed class Config : IEnumerable
{
public static readonly string ConfigPath;
static Config()
{
ConfigPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, " Rocky.config " );
}
private XmlNode appNode;
public XmlNode AppNode
{
get { return appNode; }
}
public string this [ string appKey]
{
set
{
lock (appNode)
{
XmlElement xElem = (XmlElement)appNode.SelectSingleNode( " //add[@key=' " + appKey + " '] " );
if (xElem == null )
{
xElem = appNode.OwnerDocument.CreateElement( " add " );
xElem.SetAttribute( " key " , appKey);
xElem.SetAttribute( " value " , value);
appNode.AppendChild(xElem);
}
else
{
if (value == null )
{
appNode.RemoveChild(xElem);
}
else
{
xElem.SetAttribute( " value " , value);
}
}
appNode.OwnerDocument.Save(ConfigPath);
}
}
get
{
XmlElement xElem = (XmlElement)appNode.SelectSingleNode( " //add[@key=' " + appKey + " '] " );
if (xElem == null )
{
throw new NullReferenceException();
}
return xElem.GetAttribute( " value " );
}
}
public string this [ int index]
{
set
{
XmlNodeList list = appNode.SelectNodes( " //add " );
if (index >= 0 && index < list.Count)
{
lock (appNode)
{
XmlElement xElem = (XmlElement)list[index];
if (value == null )
{
xElem.ParentNode.RemoveChild(xElem);
}
else
{
xElem.SetAttribute( " value " , value);
}
appNode.OwnerDocument.Save(ConfigPath);
}
}
}
get
{
XmlNodeList list = appNode.SelectNodes( " //add " );
if (index >= 0 && index < list.Count)
{
return ((XmlElement)list[index]).GetAttribute( " value " );
}
throw new NullReferenceException();
}
}
internal Config()
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load(ConfigPath);
appNode = xDoc.SelectSingleNode( " //appSettings " );
if (appNode == null )
{
throw new ArgumentException();
}
}
public IEnumerator GetEnumerator()
{
return appNode.SelectNodes( " //add " ).GetEnumerator();
}
}
}
using System.Xml;
using System.Collections;
using System.IO;
namespace Rocky
{
public sealed class Config : IEnumerable
{
public static readonly string ConfigPath;
static Config()
{
ConfigPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, " Rocky.config " );
}
private XmlNode appNode;
public XmlNode AppNode
{
get { return appNode; }
}
public string this [ string appKey]
{
set
{
lock (appNode)
{
XmlElement xElem = (XmlElement)appNode.SelectSingleNode( " //add[@key=' " + appKey + " '] " );
if (xElem == null )
{
xElem = appNode.OwnerDocument.CreateElement( " add " );
xElem.SetAttribute( " key " , appKey);
xElem.SetAttribute( " value " , value);
appNode.AppendChild(xElem);
}
else
{
if (value == null )
{
appNode.RemoveChild(xElem);
}
else
{
xElem.SetAttribute( " value " , value);
}
}
appNode.OwnerDocument.Save(ConfigPath);
}
}
get
{
XmlElement xElem = (XmlElement)appNode.SelectSingleNode( " //add[@key=' " + appKey + " '] " );
if (xElem == null )
{
throw new NullReferenceException();
}
return xElem.GetAttribute( " value " );
}
}
public string this [ int index]
{
set
{
XmlNodeList list = appNode.SelectNodes( " //add " );
if (index >= 0 && index < list.Count)
{
lock (appNode)
{
XmlElement xElem = (XmlElement)list[index];
if (value == null )
{
xElem.ParentNode.RemoveChild(xElem);
}
else
{
xElem.SetAttribute( " value " , value);
}
appNode.OwnerDocument.Save(ConfigPath);
}
}
}
get
{
XmlNodeList list = appNode.SelectNodes( " //add " );
if (index >= 0 && index < list.Count)
{
return ((XmlElement)list[index]).GetAttribute( " value " );
}
throw new NullReferenceException();
}
}
internal Config()
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load(ConfigPath);
appNode = xDoc.SelectSingleNode( " //appSettings " );
if (appNode == null )
{
throw new ArgumentException();
}
}
public IEnumerator GetEnumerator()
{
return appNode.SelectNodes( " //add " ).GetEnumerator();
}
}
}