在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);
}
}
}
- 在web.config设置和读出自定义键值
- 关于 web.config 里自定义节点的设置和读取
- 在Web.config中注册用户控件和自定义控件
- 在Web.config中注册用户控件和自定义控件
- 在web.config设置常量
- 在web.config和IIS中设置Session过期时间
- 在web.config和IIS中设置Session过期时间
- 在web.config和app.config文件中增加自定义配置节点
- 在web.config和app.config文件中增加自定义配置节点
- 在web.config和app.config文件中增加自定义配置节点
- 在web.config和app.config文件中增加自定义配置节点的方法
- 如何读出Web.Config文件中的东东
- 在web.config里自定义配置节
- 在Web.config或App.config中的添加自定义配置
- 在Web.config或App.config中的添加自定义配置
- 在Web.config或App.config中的添加自定义配置
- 在Web.config中设置数据库连接字段
- 在代码中调整Web.config设置
- 自定义配置节处理实现个性化web.config
- javascript实例教程(12) 隐藏script代码
- javascript实例教程(13) 鼠标移过时报警
- 如何在web.config 或者 app.config 文件中配置自定义节点
- 如何运用IF语句把数据插入到不同的表中?
- 在web.config设置和读出自定义键值
- javascript实例教程(15) JS代替CGI
- web.config文件自定义配置节的使用方法
- 转的一篇求职经历经验的文章,很长,很有用,IT向,和自己的定位非常契合,第一时间收藏+分享。
- 在web.config里自定义配置节
- javascript实例教程(16) 日期函数
- javascript实例教程(17) 使用javascript的数学函数
- 在写jsp脚本时Eclipse MyEclipse 没有响应 解决方法
- javascript实例教程(18) 使用字符串函数