在net中,在System.Configuration.ConfigurationManager中,提供了几个静态方法,用来修改配置文件。
如:System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenMachineConfiguration();
获得应用程序下的配置文件,之后再用config进行操作。
如果是在web中,那就是操作webconfig了!不过现在在winform中,就成了操作app.config了。
于是,我选择了还是以操作xml的方式来修改webconfig。
这里写了几个类,主要也是模仿config的操作方式。代码如下:

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 "";
}
}
}
下面看一下界面的操作方法:

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(扫码支持作者):微信:![]() |
![]() |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· 易语言 —— 开山篇
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 【全网最全教程】使用最强DeepSeekR1+联网的火山引擎,没有生成长度限制,DeepSeek本体