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

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

<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"];