一个不错读取Configuration文件的类,方便设置配置文件
2005-09-14 13:25
691 查看
好久不贴东西了,随便贴些,增加点人气!
类:Config.cs
using System;
using System.Xml;
using System.Reflection;
using System.Configuration;
using System.Windows.Forms;
namespace XmlConfig
{
public class Config : System.Configuration.AppSettingsReader
{
private XmlNode node;
private string _cfgFile;
public string cfgFile
{
get { return _cfgFile; }
set { _cfgFile= Application.StartupPath + "\\" +
value; }
}
public string GetValue (string key)
{
return Convert.ToString(GetValue(key, typeof(string)));
}
public new object GetValue (string key, System.Type sType)
{
XmlDocument doc = new XmlDocument();
object ro = String.Empty;
loadDoc(doc);
string sNode = key.Substring(0, key.LastIndexOf("//"));
// retrieve the selected node
try
{
node = doc.SelectSingleNode(sNode);
if( node != null )
{
// Xpath selects element that
contains the key
XmlElement targetElem=
(XmlElement)node.SelectSingleNode(key) ;
if (targetElem!=null)
{
ro =
targetElem.GetAttribute("value");
}
}
if (sType == typeof(string))
return Convert.ToString(ro);
else
if (sType == typeof(bool))
{
if (ro.Equals("True") ||
ro.Equals("False"))
return
Convert.ToBoolean(ro);
else
return false;
}
else
if (sType == typeof(int))
return Convert.ToInt32(ro);
else
if (sType == typeof(double))
return Convert.ToDouble(ro);
else
if (sType == typeof(DateTime))
return Convert.ToDateTime(ro);
else
return Convert.ToString(ro);
}
catch
{
return String.Empty;
}
}
public bool SetValue (string key, string val)
{
XmlDocument doc = new XmlDocument();
loadDoc(doc);
try
{
// retrieve the target node
string sNode = key.Substring(0,
key.LastIndexOf("//"));
node = doc.SelectSingleNode(sNode);
if( node == null )
return false;
// Set element that contains the key
XmlElement targetElem= (XmlElement)
node.SelectSingleNode(key);
if (targetElem!=null)
{
// set new value
targetElem.SetAttribute("value",
val);
}
// create new element with
key/value pair and add it
else
{
sNode =
key.Substring(key.LastIndexOf("//")+2);
XmlElement entry =
doc.CreateElement(sNode.Substring(0, sNode.IndexOf("[@")).Trim());
sNode =
sNode.Substring(sNode.IndexOf("'")+1);
entry.SetAttribute("key",
sNode.Substring(0, sNode.IndexOf("'")) );
entry.SetAttribute("value", val);
node.AppendChild(entry);
}
saveDoc(doc, this._cfgFile);
return true;
}
catch
{
return false;
}
}
private void saveDoc (XmlDocument doc, string docPath)
{
// save document
// choose to
ignore if web.config since it may cause server sessions interruptions
if( this._cfgFile.Equals("web.config") )
return;
else
try
{
XmlTextWriter writer = new
XmlTextWriter( docPath , null );
writer.Formatting =
Formatting.Indented;
doc.WriteTo( writer );
writer.Flush();
writer.Close();
return;
}
catch
{}
}
public bool removeElement (string key)
{
XmlDocument doc = new XmlDocument();
loadDoc(doc);
try
{
string sNode = key.Substring(0,
key.LastIndexOf("//"));
// retrieve the appSettings node
node = doc.SelectSingleNode(sNode);
if( node == null )
return false;
// XPath select setting "add" element that contains
this key to remove
XmlNode nd = node.SelectSingleNode(key);
node.RemoveChild(nd);
saveDoc(doc, this._cfgFile);
return true;
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
}
private void loadDoc ( XmlDocument doc )
{
// check for type of config file being requested
/*
if( this._cfgFile.Equals("app.config"))
{
// use default app.config
this._cfgFile =
((Assembly.GetEntryAssembly()).GetName()).Name+".exe.config";
}
else
if( this._cfgFile.Equals("web.config"))
{
// use server web.config
this._cfgFile =
System.Web.HttpContext.Current.Server.MapPath("web.config");
}
*/
// load the document
doc.Load(this._cfgFile );
}
}
}
使用:
取:
写:
删除:
详情请参阅:http://www.codeproject.com/csharp/XmlConfig.asp
类:Config.cs
using System;
using System.Xml;
using System.Reflection;
using System.Configuration;
using System.Windows.Forms;
namespace XmlConfig
{
public class Config : System.Configuration.AppSettingsReader
{
private XmlNode node;
private string _cfgFile;
public string cfgFile
{
get { return _cfgFile; }
set { _cfgFile= Application.StartupPath + "\\" +
value; }
}
public string GetValue (string key)
{
return Convert.ToString(GetValue(key, typeof(string)));
}
public new object GetValue (string key, System.Type sType)
{
XmlDocument doc = new XmlDocument();
object ro = String.Empty;
loadDoc(doc);
string sNode = key.Substring(0, key.LastIndexOf("//"));
// retrieve the selected node
try
{
node = doc.SelectSingleNode(sNode);
if( node != null )
{
// Xpath selects element that
contains the key
XmlElement targetElem=
(XmlElement)node.SelectSingleNode(key) ;
if (targetElem!=null)
{
ro =
targetElem.GetAttribute("value");
}
}
if (sType == typeof(string))
return Convert.ToString(ro);
else
if (sType == typeof(bool))
{
if (ro.Equals("True") ||
ro.Equals("False"))
return
Convert.ToBoolean(ro);
else
return false;
}
else
if (sType == typeof(int))
return Convert.ToInt32(ro);
else
if (sType == typeof(double))
return Convert.ToDouble(ro);
else
if (sType == typeof(DateTime))
return Convert.ToDateTime(ro);
else
return Convert.ToString(ro);
}
catch
{
return String.Empty;
}
}
public bool SetValue (string key, string val)
{
XmlDocument doc = new XmlDocument();
loadDoc(doc);
try
{
// retrieve the target node
string sNode = key.Substring(0,
key.LastIndexOf("//"));
node = doc.SelectSingleNode(sNode);
if( node == null )
return false;
// Set element that contains the key
XmlElement targetElem= (XmlElement)
node.SelectSingleNode(key);
if (targetElem!=null)
{
// set new value
targetElem.SetAttribute("value",
val);
}
// create new element with
key/value pair and add it
else
{
sNode =
key.Substring(key.LastIndexOf("//")+2);
XmlElement entry =
doc.CreateElement(sNode.Substring(0, sNode.IndexOf("[@")).Trim());
sNode =
sNode.Substring(sNode.IndexOf("'")+1);
entry.SetAttribute("key",
sNode.Substring(0, sNode.IndexOf("'")) );
entry.SetAttribute("value", val);
node.AppendChild(entry);
}
saveDoc(doc, this._cfgFile);
return true;
}
catch
{
return false;
}
}
private void saveDoc (XmlDocument doc, string docPath)
{
// save document
// choose to
ignore if web.config since it may cause server sessions interruptions
if( this._cfgFile.Equals("web.config") )
return;
else
try
{
XmlTextWriter writer = new
XmlTextWriter( docPath , null );
writer.Formatting =
Formatting.Indented;
doc.WriteTo( writer );
writer.Flush();
writer.Close();
return;
}
catch
{}
}
public bool removeElement (string key)
{
XmlDocument doc = new XmlDocument();
loadDoc(doc);
try
{
string sNode = key.Substring(0,
key.LastIndexOf("//"));
// retrieve the appSettings node
node = doc.SelectSingleNode(sNode);
if( node == null )
return false;
// XPath select setting "add" element that contains
this key to remove
XmlNode nd = node.SelectSingleNode(key);
node.RemoveChild(nd);
saveDoc(doc, this._cfgFile);
return true;
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
}
private void loadDoc ( XmlDocument doc )
{
// check for type of config file being requested
/*
if( this._cfgFile.Equals("app.config"))
{
// use default app.config
this._cfgFile =
((Assembly.GetEntryAssembly()).GetName()).Name+".exe.config";
}
else
if( this._cfgFile.Equals("web.config"))
{
// use server web.config
this._cfgFile =
System.Web.HttpContext.Current.Server.MapPath("web.config");
}
*/
// load the document
doc.Load(this._cfgFile );
}
}
}
使用:
取:
Config config = new Config(); config.cfgFile = "app.config"; txtCountry.Text = config.GetValue("//appSettings//add[@key='CountryLoc']");
写:
config.SetValue("//appSettings//add[@key='" + txtKey.Text + "']", txtValue.Text);
删除:
config.removeElement("//appSettings//add[@key='" + txtKey2.Text + "']");
详情请参阅:http://www.codeproject.com/csharp/XmlConfig.asp
相关文章推荐
- 一个不错读取Configuration文件的类,方便设置配置文件
- 一个不错的java的配置文件的设置
- python的ConfigParser读取设置配置文件
- SourceInsight一个不错的配置文件下载地址
- c++/cli 读取配置文件app.config所需的设置
- 记录一个 spring cloud 配置中心的坑,命令行端口参数无效,被覆盖,编码集问题无法读取文件等.
- 用python写的一个超小型配置文件读取器
- 用一个随项目启动而初始化的servlet读取配置环境,参数的properties文件
- SpringBoot学习——如何设置和读取配置文件中属性
- 几种从配置文件中读取数据,设置参数的方法
- 读取关键字配置文件,并将关键字放入到一个列表中(去重复去和空格)--PYTHON
- 一个方便的读取GML文件 方法
- Java配置文件读取和路径设置
- 用cocos2d3.0写一个srpg游戏-从配置文件中读取敌我出战阵容(一)
- 用java读取解析一个含有变量的配置文件
- 一个常用工具类,读取配置文件
- Apache Common Configuration--超方便的配置文件读取解析
- 读取、设置 php.ini配置文件(复制)
- 用Perl写一个读取nginx配置文件到csv,实现域名与IP对应的实用程序
- java读取和设置配置文件中的属性参数