在net中,在System.Configuration.ConfigurationManager中,提供了几个静态方法,用来修改配置文件。
如:System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenMachineConfiguration();
获得应用程序下的配置文件,之后再用config进行操作。
如果是在web中,那就是操作webconfig了!不过现在在winform中,就成了操作app.config了。
于是,我选择了还是以操作xml的方式来修改webconfig。
这里写了几个类,主要也是模仿config的操作方式。代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
namespace IISHelper
{
public class WebConfigHelper : IDisposable
{
private bool loadIsOK;
/// <summary>
/// 加载是否成功
/// </summary>
public bool LoadIsOK
{
get { return loadIsOK; }
set { loadIsOK = value; }
}
private XmlDocument xDox = new XmlDocument();
private string configPath = string.Empty;
public WebConfigHelper(string webconfigPath)
{
try
{
xDox.Load(webconfigPath);
configPath = webconfigPath;
loadIsOK = true;
}
catch { loadIsOK = false; }
}
public WebConfigAppSetting AppSetting
{
get
{
XmlNode xNode=xDox.SelectSingleNode("//configuration/appSettings");
if(xNode==null)
{
return null;
}
return new WebConfigAppSetting(ref xNode);
}
}
public WebConfigConnectionStrings ConnectionStrings
{
get
{
XmlNode xNode = xDox.SelectSingleNode("//configuration/connectionStrings");
if (xNode == null)
{
return null;
}
return new WebConfigConnectionStrings(ref xNode);
}
}
public bool Save()
{
try
{
xDox.Save(configPath);
return true;
}
catch { }
return false;
}
#region IDisposable 成员
public void Dispose()
{
xDox = null;
}
#endregion
}
public abstract class WebConfigBase
{
protected XmlNode node;
public void Add(string key, string value){}
public abstract void Set(string key, string value);
public abstract string Get(string key);
public void Remove(string key, string value){}
}
public class WebConfigAppSetting : WebConfigBase
{
internal WebConfigAppSetting(ref XmlNode xNode)
{
node = xNode;
}
public override void Set(string key, string value)
{
foreach (XmlNode addNode in node.ChildNodes)
{
if (addNode.Attributes != null && addNode.Attributes["key"].Value == key)
{
addNode.Attributes["value"].Value = value;
break;
}
}
}
public override string Get(string key)
{
foreach (XmlNode addNode in node.ChildNodes)
{
if (addNode.Attributes != null && addNode.Attributes["key"].Value == key)
{
return addNode.Attributes["value"].Value;
}
}
return "";
}
}
public class WebConfigConnectionStrings : WebConfigBase
{
internal WebConfigConnectionStrings(ref XmlNode xNode)
{
node = xNode;
}
public override void Set(string key, string value)
{
foreach (XmlNode addNode in node.ChildNodes)
{
if (addNode.Attributes != null && addNode.Attributes["name"].Value == key)
{
addNode.Attributes["connectionString"].Value = value;
break;
}
}
}
public override string Get(string key)
{
foreach (XmlNode addNode in node.ChildNodes)
{
if (addNode.Attributes != null && addNode.Attributes["name"].Value == key)
{
return addNode.Attributes["connectionString"].Value;
}
}
return "";
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Xml;
namespace IISHelper
{
public class WebConfigHelper : IDisposable
{
private bool loadIsOK;
/// <summary>
/// 加载是否成功
/// </summary>
public bool LoadIsOK
{
get { return loadIsOK; }
set { loadIsOK = value; }
}
private XmlDocument xDox = new XmlDocument();
private string configPath = string.Empty;
public WebConfigHelper(string webconfigPath)
{
try
{
xDox.Load(webconfigPath);
configPath = webconfigPath;
loadIsOK = true;
}
catch { loadIsOK = false; }
}
public WebConfigAppSetting AppSetting
{
get
{
XmlNode xNode=xDox.SelectSingleNode("//configuration/appSettings");
if(xNode==null)
{
return null;
}
return new WebConfigAppSetting(ref xNode);
}
}
public WebConfigConnectionStrings ConnectionStrings
{
get
{
XmlNode xNode = xDox.SelectSingleNode("//configuration/connectionStrings");
if (xNode == null)
{
return null;
}
return new WebConfigConnectionStrings(ref xNode);
}
}
public bool Save()
{
try
{
xDox.Save(configPath);
return true;
}
catch { }
return false;
}
#region IDisposable 成员
public void Dispose()
{
xDox = null;
}
#endregion
}
public abstract class WebConfigBase
{
protected XmlNode node;
public void Add(string key, string value){}
public abstract void Set(string key, string value);
public abstract string Get(string key);
public void Remove(string key, string value){}
}
public class WebConfigAppSetting : WebConfigBase
{
internal WebConfigAppSetting(ref XmlNode xNode)
{
node = xNode;
}
public override void Set(string key, string value)
{
foreach (XmlNode addNode in node.ChildNodes)
{
if (addNode.Attributes != null && addNode.Attributes["key"].Value == key)
{
addNode.Attributes["value"].Value = value;
break;
}
}
}
public override string Get(string key)
{
foreach (XmlNode addNode in node.ChildNodes)
{
if (addNode.Attributes != null && addNode.Attributes["key"].Value == key)
{
return addNode.Attributes["value"].Value;
}
}
return "";
}
}
public class WebConfigConnectionStrings : WebConfigBase
{
internal WebConfigConnectionStrings(ref XmlNode xNode)
{
node = xNode;
}
public override void Set(string key, string value)
{
foreach (XmlNode addNode in node.ChildNodes)
{
if (addNode.Attributes != null && addNode.Attributes["name"].Value == key)
{
addNode.Attributes["connectionString"].Value = value;
break;
}
}
}
public override string Get(string key)
{
foreach (XmlNode addNode in node.ChildNodes)
{
if (addNode.Attributes != null && addNode.Attributes["name"].Value == key)
{
return addNode.Attributes["connectionString"].Value;
}
}
return "";
}
}
}
下面看一下界面的操作方法:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
WebConfigHelper allConfig = new WebConfigHelper(allConfigPath);
if (allConfig.LoadIsOK)
{
WebConfigAppSetting app = allConfig.AppSetting;
if (app != null)
{
app.Set("MosFTPUserName", txtMosFtpUserName.Text);
app.Set("MosFTPPassword", txtMosFtpPassword.Text);
app.Set("ProvideFor", cbbProvideFor.Text);
}
WebConfigConnectionStrings connString = allConfig.ConnectionStrings;
if (connString != null)
{
connString.Set("Conn", txtConn.Text);
}
allConfig.Save();
allConfig.Dispose();
MessageBox.Show("配置文件修改成功!");
}
if (allConfig.LoadIsOK)
{
WebConfigAppSetting app = allConfig.AppSetting;
if (app != null)
{
app.Set("MosFTPUserName", txtMosFtpUserName.Text);
app.Set("MosFTPPassword", txtMosFtpPassword.Text);
app.Set("ProvideFor", cbbProvideFor.Text);
}
WebConfigConnectionStrings connString = allConfig.ConnectionStrings;
if (connString != null)
{
connString.Set("Conn", txtConn.Text);
}
allConfig.Save();
allConfig.Dispose();
MessageBox.Show("配置文件修改成功!");
}
这里提示一下,web.config中,不要带名称空间,就是xmlns="xxxx一大堆的";
打完,收工!
版权声明:本文原创发表于 博客园,作者为 路过秋天 本文欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则视为侵权。 |
个人微信公众号![]() |
Donation(扫码支持作者):支付宝:![]() |
Donation(扫码支持作者):微信:![]() |
![]() |