最近使用配置文件,需要删除修改,所以找了个类。。。
类库如下:
using System;
using System.Configuration;
using System.Reflection;
using System.Web;
using System.Xml;
public enum ConfigFileType
![]()
![]()
{
WebConfig,
AppConfig
}
![]()
![]()
/**//// <summary>
/// Summary description for ReadWriteConfig.
/// </summary>
public class ReadWriteConfig
![]()
![]()
{
public string docName = String.Empty;
private XmlNode node = null;
private int _configType;
public int ConfigType
![]()
{
![]()
get
{ return _configType; }
![]()
set
{ _configType = value; }
}
![]()
![]()
SetValue#region SetValue
public bool SetValue(string key, string value)
![]()
{
XmlDocument cfgDoc = new XmlDocument();
loadConfigDoc(cfgDoc);
// retrieve the appSettings node
node = cfgDoc.SelectSingleNode("//appSettings");
if (node == null)
![]()
{
throw new InvalidOperationException("appSettings section not found");
}
try
![]()
{
// XPath select setting "add" element that contains this key
XmlElement addElem = (XmlElement)node.SelectSingleNode("//add[@key='" + key + "']");
if (addElem != null)
![]()
{
addElem.SetAttribute("value", value);
}
// not found, so we need to add the element, key and value
else
![]()
{
XmlElement entry = cfgDoc.CreateElement("add");
entry.SetAttribute("key", key);
entry.SetAttribute("value", value);
node.AppendChild(entry);
}
//save it
saveConfigDoc(cfgDoc, docName);
return true;
}
catch
![]()
{
return false;
}
}
![]()
#endregion
![]()
![]()
saveConfigDoc#region saveConfigDoc
private void saveConfigDoc(XmlDocument cfgDoc, string cfgDocPath)
![]()
{
try
![]()
{
XmlTextWriter writer = new XmlTextWriter(cfgDocPath, null);
writer.Formatting = Formatting.Indented;
cfgDoc.WriteTo(writer);
writer.Flush();
writer.Close();
return;
}
catch
![]()
{
throw;
}
}
![]()
public string readConfigDoc(string elementKey)
![]()
{
try
![]()
{
XmlDocument cfgDoc = new XmlDocument();
loadConfigDoc(cfgDoc);
// retrieve the appSettings node
node = cfgDoc.SelectSingleNode("//appSettings");
if (node == null)
![]()
{
throw new InvalidOperationException("appSettings section not found");
}
XmlElement addElem = (XmlElement)node.SelectSingleNode("//add[@key='" + elementKey + "']");
![]()
if (addElem != null)
![]()
{
return addElem.GetAttribute("value");
}
// not found, so we need to add the element, key and value
else
![]()
{
return "";
}
}
catch
![]()
{
return "";
}
}
![]()
//public string readConfigDoc(XmlDocument cfgDoc)
//{
// try
// {
// XmlTextReader read = new XmlTextReader(cfgDoc);
// return read.ReadContentAsString();
// }
// catch
// {
// throw;
// }
//}
#endregion
![]()
![]()
removeElement#region removeElement
public bool removeElement(string elementKey)
![]()
{
try
![]()
{
XmlDocument cfgDoc = new XmlDocument();
loadConfigDoc(cfgDoc);
// retrieve the appSettings node
node = cfgDoc.SelectSingleNode("//appSettings");
if (node == null)
![]()
{
throw new InvalidOperationException("appSettings section not found");
}
// XPath select setting "add" element that contains this key to remove
node.RemoveChild(node.SelectSingleNode("//add[@key='" + elementKey + "']"));
saveConfigDoc(cfgDoc, docName);
return true;
}
catch
![]()
{
return false;
}
}
#endregion
![]()
![]()
modifyElement#region modifyElement
public bool modifyElement(string elementKey)
![]()
{
try
![]()
{
XmlDocument cfgDoc = new XmlDocument();
loadConfigDoc(cfgDoc);
// retrieve the appSettings node
node = cfgDoc.SelectSingleNode("//appSettings");
if (node == null)
![]()
{
throw new InvalidOperationException("appSettings section not found");
}
// XPath select setting "add" element that contains this key to remove
node.RemoveChild(node.SelectSingleNode("//add[@key='" + elementKey + "']"));
saveConfigDoc(cfgDoc, docName);
return true;
}
catch
![]()
{
return false;
}
}
#endregion
![]()
![]()
loadConfigDoc#region loadConfigDoc
private XmlDocument loadConfigDoc(XmlDocument cfgDoc)
![]()
{
// load the config file
if (Convert.ToInt32(ConfigType) == Convert.ToInt32(ConfigFileType.AppConfig))
![]()
{
docName = ((Assembly.GetEntryAssembly()).GetName()).Name;
docName += ".exe.config";
}
else
![]()
{
docName = HttpContext.Current.Server.MapPath("~/System.config");
}
cfgDoc.Load(docName);
return cfgDoc;
}
#endregion
}
![]()
System.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="UpLoadImage" value="http://localhost:4332/Admin/" />
<add key="EditUpLoadImage" value="JPG|GIF|PNG" />
<add key="UpLoadFiles" value="http://***:8008/UpLoadFiles/" />
<add key="FileTypeAllow" value="JPG|GIF|TXT|DOC" />
<add key="UpLoadSmallImage" value="http://***:8008/Smallimages/" />
<add key="UpLoadBigImages" value="http://***:8008/Bigimages/" />
<add key="DataBackPath" value="D:\SQL_Data\data_bck_test" />
<add key="CacheLength" value="1|h" />
<add key="FilesCount" value="5" />
</appSettings>
<connectionStrings />
<system.web>
</system.web>
</configuration>
使用:
读:
ReadWriteConfig config = new ReadWriteConfig();
config.ConfigType = (int)ConfigFileType.WebConfig;
txtImageTypeAllow.Text = config.readConfigDoc("EditUpLoadImage");
![]()
写:
bool b = false;
ReadWriteConfig config = new ReadWriteConfig();
config.ConfigType = (int)ConfigFileType.WebConfig;
b = config.SetValue("UpLoadFiles", txtFilePath.Text);
删除使用 removeElement(...)函数