将数据保存在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
- 将数据保存在web.config配置文件中,及如何获取config配置文件中的数据
- Web.config保存数据
- web.config配置文件如何设置
- web.config配置文件中的 元素
- WCF中的web.config配置文件
- 如何在Web和桌面程序统一配置文件XXX.Config
- WPF读写config配置文件及XML数据的学习
- C#获取web.config配置文件内容
- C#获取web.config配置文件内容
- C#获取web.config配置文件内容
- C#获取web.config配置文件内容
- C#获取web.config配置文件内容
- C#获取web.config配置文件内容
- atlas Web.Config 配置文件
- web.config配置文件示例
- web.config配置文件
- web.config 配置文件
- web.config 配置文件
- keychain
- 动态内存管理详解:malloc/free/new/delete/brk/mmap
- Liferay6.2开发之使用Search-Container
- Sales_item.h (C++ Primer 第五版)
- 自定义控件其实很简单7/12
- 将数据保存在web.config配置文件中,及如何获取config配置文件中的数据
- 用百度map api展示时空数据(五)--老地图的添加与删除
- IOS 学习资料收集( 2015 -2-13 日不定时收集)
- 汇编语言---函数调用栈
- C++11 lambda表达式
- SZUOJ-Problem(A60):Roads
- Ubuntu下搭建Spark+Scala+idea-IC+R+Rstudio实践记录
- Android 中fill_parent、wrap_content和match_parent的区别
- 第2章 2.9JSP脚本中的9个内置对象