将数据保存在web.config配置文件中,及如何获取config配置文件中的数据

来源:互联网 发布:linux c select 编辑:程序博客网 时间:2024/06/05 12:49

<1>

有的数据需要写到配置文件中的。我们就尽量写到配置文件中来。比如经常变动的数据 ,或者用户时候的时候只要改改配置文件就可以了用了的值,如:ip地址。端口号,MD5加盐。等等。我们可以将这些值写入到web.config文件中来。在webForm.aspx.cs页面,或者其他页面去获取这个值就可以了

注意是在<appSettings></appSettings>文件中进行配置。

<?xml version="1.0" encoding="utf-8"?><!--  有关如何配置 ASP.NET 应用程序的详细消息,请访问  http://go.microsoft.com/fwlink/?LinkId=169433  --><configuration>    <system.web>        <compilation debug="true" targetFramework="4.0" />    </system.web>  <connectionStrings>    <add name="ConnStr" connectionString="Data Source=Fa-VAIO; Initial Catalog=sales; Integrated Security=True"/>     </connectionStrings><!--有的数据需要写到配置文件中的。我们就尽量写到配置文件中来。比如经常变动的数据 ,或者用户时候的时候只要改改配置文件就可以了用了的值,如:ip地址。端口号,MD5加盐。等等。我们可以将这些值写入到web.config文件中来。在webForm.aspx.cs页面,或者其他页面去获取这个值就可以了-->  <appSettings>    <add key="IP" value="192.168.1.199"/>    <add key="端口号" value="8080"/>    <add key="MD5加盐" value="我的盐"/>  </appSettings></configuration>

webForm.aspx.cs 文件中来获取

using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Configuration;namespace 用户激活{    public partial class WebForm4 : System.Web.UI.Page    {        protected void Page_Load(object sender, EventArgs e)        {            string  getIP= ConfigurationManager.AppSettings["IP"];            string duankouhao = ConfigurationManager.AppSettings["端口号"];            string mySalt = ConfigurationManager.AppSettings["MD5加盐"];        }    }}


static readonly string smtpServer = System.Configuration.ConfigurationManager.AppSettings["SmtpServer"];        static readonly string userName = System.Configuration.ConfigurationManager.AppSettings["UserName"];        static readonly string pwd = System.Configuration.ConfigurationManager.AppSettings["Pwd"];        static readonly int smtpPort = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["SmtpPort"]);        static readonly string authorName = System.Configuration.ConfigurationManager.AppSettings["AuthorName"];        static readonly string to = System.Configuration.ConfigurationManager.AppSettings["To"];

---------------------------------------------------------------------------------------------------------------

获取自定义节点中的数据

首先定义一个类:ContactUsSectionHandler.cs

using System;using System.Collections.Generic;using System.Configuration;using System.Linq;using System.Web;using System.Xml;namespace MvcApp.Models{    public class ContactUsSectionHandler : IConfigurationSectionHandler    {        private XmlNode _sectionNode; //用来保存配置文件中的节点        public XmlNode SectionNode        {            get { return _sectionNode; }        }        //当调用ConfigurationManager.GetSection("ContactUsSection")方法时,如果某个类继承IConfigurationSectionHandler接口,那么会触发此接口的Create方法,此时ConfigurationManager.GetSection("ContactUsSection")获取到的Web.config配置文件中的ContactUsSection节点就会传递到 Create方法的第三个参数section上。所以此时section中保存的内容就是Web,config配置文件中的ContactUsSection这个节点的内容        public virtual object Create(object paarnet, object context, XmlNode section)//实现IConfigurationSectionHandler接口中的Create方法        {            this._sectionNode = section; //初始化私有成员_sectionNode            return this; //这个this 其实就是 MvcApp.Models.ContactUsSectionHandler 类的对象。因为我已经在Create方法中将Web.config配置文件中的ContactUsSection这个节点赋值给当前类ContactUsSectionHandler的一个私有属性_sectionNode了。 所以这里这里就可以返回当前类的一个对象啦。        }    }}

第二在<configSections>节点中声明一个自定义节点:

  <configSections >    <!--既然要自定义一个ContactUsSection节点,就需要先在<configSections></configSections>这个节点里声明一下自定义节点的名称及类型-->    <!--这个type表示节点的类型是什么,其实就是表明如果你要获取这个节点的时候,用哪个类来保存这个ContactUsSection节点的内容。 我这里是用MvcApp项目下的Models文件夹下的ContactUsSectionHandler类来保存ContactUsSection这个节点的内容。逗号后面的MvcApp是项目名称-->    <section name="ContactUsSection" type="MvcApp.Models.ContactUsSectionHandler,MvcApp"/>  </configSections>

然后再<configuration>节点下 配置自定义节点

 <ContactUsSection>        <ContactUs>      <SMTP host="smtp.qq.com" port="587" EnableSsl="true" UseDefaultCredentials="">        <From password="123456" displayname="">12345678910@qq.com</From>        <To>98765432101@qq.com</To>        <Cc>mono@163.com</Cc>        <Bcc></Bcc>      </SMTP></ContactUs></configSections>


最后,获取自定义节点中的信息

//调用ConfigurationManager.GetSection("ContactUsSection")方法,获取Web.config配置文件中的ContactUsSection节点的内容,//注意:Web.config配置文件中的ContactUsSection节点的内容保存在ContactUsSectionHandler类的私有属性_sectionNode中//注意:_sectionNode属性的类型是XmlNode类型的,所以它可以保存ContactUsSection节点的内容ContactUsSectionHandler section = System.Configuration.ConfigurationManager.GetSection("ContactUsSection") as ContactUsSectionHandler;

获取<configuration>下的节点中的数据

<?xml version="1.0" encoding="utf-8" ?><configuration>  <connectionStrings>    <add name="getConn" connectionString="data source=FFB-VAIO;initial catalog=sales;integrated security=true"/>  </connectionStrings></configuration>

//获取<configuration>下节点的信息string connStr = ConfigurationManager.ConnectionStrings["getConn"].ConnectionString;SqlConnection m_SqlConnection = new SqlConnection(connStr);

获取<appSettings>节点中的数据

<appSettings>    <add key="Name" value="张三" />    <add key="Age" value="26" />  </appSettings>
//获取<appSettings>节点中的内容,用以下方式string Name = ConfigurationManager.AppSettings["Name"];



0 0
原创粉丝点击