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);
}