在web.config设置和读出自定义键值

来源:互联网 发布:大学生分期软件有哪些 编辑:程序博客网 时间:2024/05/29 08:53

在web.config设置和读出自定义键值
web.config如下
----------------------------------------------------------
<?xml version="1.0" encoding="gb2312" ?>
<configuration>
 <appSettings>
  <add key="ConnStr" value="data source=localhost;initial catalog=H_bbyv;integrated security=SSPI;persist security info=False;workstation id=BBYV;packet size=4096" />
  <add key="Admin" value="name"></add>
  <!-- 如果是敏感的内容如密码最好不要明文保存,可以用如下方法加密后才保存。
public static string HashPasswordForStoringInConfigFile(
   string password,
   string passwordFormat
)
-->
  <add key="Password" value="password"></add>
  <!-- 在这里加入更多的<add key="KeyName" value="KeyValue" /> -->
 </appSettings>
</configuration>
----------------------------------------------------------
当我们设置了web.config后,就可以在需要时方便地读出。
----------------------------------------------------------
using System.Configuration;

string connStr = ConfigurationSettings.AppSettings["ConnStr"];
string admin = ConfigurationSettings.AppSettings["Admin"];
string password = ConfigurationSettings.AppSettings["Password"];
---------------------------------------------------------------
修改值代码如下,但一旦修改了web.config会导致整个webapplication的重启,session和cookie也会无效,需要严重注意!所以如需经常修改就不要保存在根目录下的web.config下了。或者尝试保存在一个叫myweb.config下。但是web.config会自动缓冲,速度会有所提高。
ModifyCustomKeyValue.aspx
-----------------------------------------------------------------
<%@ Page language="c#" Codebehind="ModifyCustomKeyValue.aspx.cs" AutoEventWireup="false" Inherits="TestWeb.ModifyCustomKeyValue" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>ModifyCustomKeyValue</title>
  <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
  <meta name="CODE_LANGUAGE" Content="C#">
  <meta name="vs_defaultClientScript" content="JavaScript">
  <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
 </HEAD>
 <body>
  <form id="ModifyCustomKeyValue" method="post" runat="server">
   Key<asp:TextBox id="tbKey" runat="server"></asp:TextBox>(注意大小写)
   <br>
   Value<asp:TextBox id="tbValue" runat="server"></asp:TextBox>
   <br>
   <asp:Button id="btnModity" runat="server" Text="修改"></asp:Button>
   <br>
   转换内容<asp:TextBox id="tbContent" runat="server"></asp:TextBox><br>
   转换后内容<asp:TextBox id="tbConvertContent" runat="server"></asp:TextBox><br>
   <asp:RadioButtonList id="rblMode" runat="server">
    <asp:ListItem Value="md5">MD5</asp:ListItem>
    <asp:ListItem Value="sha1">SHA1</asp:ListItem>
   </asp:RadioButtonList><br>
   <asp:Button id="btnConvert" runat="server" Text="转换"></asp:Button>
  </form>
 </body>
</HTML>

---------------------------------------------------------------------
ModifyCustomKeyValue.aspx.cs
----------------------------------------------------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml;

namespace TestWeb {
 /// <summary>
 /// ModifyCustomKeyValue 的摘要说明。
 /// </summary>
 public class ModifyCustomKeyValue : System.Web.UI.Page {
  protected System.Web.UI.WebControls.TextBox tbKey;
  protected System.Web.UI.WebControls.TextBox tbValue;
  protected System.Web.UI.WebControls.TextBox tbContent;
  protected System.Web.UI.WebControls.TextBox tbConvertContent;
  protected System.Web.UI.WebControls.Button btnConvert;
  protected System.Web.UI.WebControls.RadioButtonList rblMode;
  protected System.Web.UI.WebControls.Button btnModity;
 
  private void Page_Load(object sender, System.EventArgs e) {
   // 在此处放置用户代码以初始化页面
  }

  #region Web Form Designer generated code
  override protected void OnInit(EventArgs e) {
   //
   // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent() {   
   this.btnModity.Click += new System.EventHandler(this.btnModity_Click);
   this.btnConvert.Click += new System.EventHandler(this.btnConvert_Click);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  private void btnModity_Click(object sender, System.EventArgs e) {

   string file = this.MapPath(this.Request.ApplicationPath + "//web.config");
   XmlDocument document = new XmlDocument();
   document.Load(file);
   XmlNode node = document.SelectSingleNode("//add[@key = '" + this.tbKey.Text.Trim() + "']");
   node.Attributes["value"].Value = this.tbValue.Text.Trim();
   document.Save(file);

   Response.Write("修改成功");
  }

  private void btnConvert_Click(object sender, System.EventArgs e) {

   tbConvertContent.Text = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(tbContent.Text.Trim(), rblMode.SelectedItem.Value);
  
  }
 }
}

 
原创粉丝点击