如何编程操作Web.config文件中的数据

1. 如果将数据加入到appSettings节中

web.config中:

<appsettings><br><add key="ConnectionString" value="data source=(local);initial catalog=yourdata;user id=sa;password=;"><br></add><br></appsettings>

web application中:

NameValueCollection col = ConfigurationSettings.AppSettings;
string connstr= col.Get("ConnectionString");

System.Configuration.ConfigurationSettings.AppSettings["SomeName"]

2.自定义的数据
需要两部,
1.在web.config文件中添加自定义的数据。

<configsections><br><!--sectionGroup name="expServerSettings"--><br><section name="cameraServers" type="System.Configuration.NameValueSectionHandler"></section><br><section name="expServers" type="System.Configuration.NameValueSectionHandler"></section><br><!--/sectionGroup--><br></configsections>
<!--expServerSettings-->
<cameraservers><br><add key="robotArm" value="10.1.2.13:1218"></add><br><add key="helicopter" value="10.1.2.12:8000"></add><br></cameraservers>
<expservers><br><add key="robotArm" value="10.1.2.15:3000"></add><br><add key="helicopter" value="10.1.2.16:3000"></add><br></expservers>
<!--/expServerSettings-->

读取代码

NameValueCollection section = System.Configuration.ConfigurationManager.GetSection("expServers") as NameValueCollection;
if (section != null)
{
robotArmServer.Text = section["robotArm"];
helicopter.Text = section["helicopter"];
}
else
{
robotArmServer.Text = "Unknown";
helicopter.Text = "Unknown";
}
section = System.Configuration.ConfigurationManager.GetSection("cameraServers") as NameValueCollection;

if (section != null)
{
robotArmCamera.Text = section["robotArm"];
heliCamera.Text = section["helicopter"];
}
else
{
robotArmCamera.Text = "Unknown";
heliCamera.Text = "Unknown";
}

注意到这里的sectionGroup被注释掉了,如何使用sectionGroup呢?这里暂且不说,如何修改数据呢?

3。修改配置文件

public void Modify(string key, string strValue) //两个参数:要修改的键值 和 要修改的新值;
{
string XPath = "/configuration/userInfo/add[@key='?']";
XmlDocument domWebConfig = new XmlDocument();

domWebConfig.Load((HttpContext.Current.Server.MapPath("web.config")));
XmlNode addKey = domWebConfig.SelectSingleNode((XPath.Replace("?", key)));
if (addKey == null)
{
Response.Write("<script>alert (/"没有找到<add key='" + key + "' value=.../>的配置节/")</script>");
return;
}
addKey.Attributes["value"].InnerText = strValue;
domWebConfig.Save((HttpContext.Current.Server.MapPath("web.config")));

}

4。使用DataSet对配置文件进行读写,这种方法虽不是太好,也算作一种途径,可能有时更有用。

读web.config:

using System.Data;
string str_webconfig = Server.MapPath("Web.config");
DataSet dsxml = new DataSet();
dsxml.ReadXml(str_webconfig);
newsname.Text = dsxml.Tables[1].Rows[0][1].ToString();
newsurl.Text = dsxml.Tables[1].Rows[1][1].ToString();
copyright.Text = dsxml.Tables[1].Rows[2][1].ToString();

.................................
写web.config:

string str_webconfig = HttpContext.Current.Server.MapPath("Web.config");
DataSet dsxml = new DataSet();
try
{
dsxml.ReadXml(str_webconfig);
dsxml.Tables[1].Rows[0][1] = newsname.Text;
dsxml.Tables[1].Rows[1][1] = newsurl.Text;
dsxml.Tables[1].Rows[2][1] = copyright.Text;
dsxml.Tables[1].Rows[3][1] = badword.Text;
........................................
dsxml.AcceptChanges();
dsxml.WriteXml(str_webconfig);
dsxml.Clear();
}
catch (Exception exc)
{
throw new Exception(exc.Message);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值