关于web.config的读写管理

web.config是xml格式的,用xml的通用方式来读写,当然没有任何问题,但有点麻烦。

后在网上查找,并经过验证,发现用Configuration类和AppSettingsSection类可以直接实现。

下面是管理web.config页面的HTML代码:

关于web.config的读写管理关于web.config的读写管理Code
 1关于web.config的读写管理    <form id="form1" runat="server">
 2关于web.config的读写管理    <div>
 3关于web.config的读写管理        <asp:Label ID="Label1" runat="server" Text="上传模型年份最小值:"></asp:Label>
 4关于web.config的读写管理        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
 5关于web.config的读写管理        <asp:Label ID="Label2" runat="server" Text="上传模型年份最大值:"></asp:Label><asp:TextBox
 6关于web.config的读写管理            ID="TextBox2" runat="server"></asp:TextBox><br />
 7关于web.config的读写管理        <asp:Label ID="Label3" runat="server" Text="页面表格每页显示行数:"></asp:Label><asp:TextBox
 8关于web.config的读写管理            ID="TextBox3" runat="server"></asp:TextBox><br />
 9关于web.config的读写管理        <asp:Label ID="lbTime" runat="server" Text="用户登录有效时间:"></asp:Label>&nbsp;
10关于web.config的读写管理        <asp:TextBox ID="tbTime" runat="server"></asp:TextBox>
11关于web.config的读写管理        <asp:Label ID="lbTimeUnit" runat="server" Text="秒"></asp:Label>
12关于web.config的读写管理        <asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="tbTime"
13关于web.config的读写管理            Display="Dynamic" ErrorMessage="用户登录有效时间必须填写数字" ValidationGroup="group1"></asp:CustomValidator><br />
14关于web.config的读写管理        <br />
15关于web.config的读写管理        <br />
16关于web.config的读写管理        <asp:Label ID="lbAdminEmail" runat="server" Text="系统管理员邮箱:"></asp:Label>
17关于web.config的读写管理        <asp:TextBox ID="tbAdminEmail" runat="server" Width="246px"></asp:TextBox>
18关于web.config的读写管理
19关于web.config的读写管理        &nbsp; &nbsp;&nbsp;
20关于web.config的读写管理        <br />
21关于web.config的读写管理        <br />
22关于web.config的读写管理        <table style="width: 348px">
23关于web.config的读写管理            <tr>
24关于web.config的读写管理                <td rowspan="2">
25关于web.config的读写管理        <asp:DropDownList ID="DDLRoleSource" runat="server" Width="100px">
26关于web.config的读写管理        </asp:DropDownList></td>
27关于web.config的读写管理                <td>
28关于web.config的读写管理        <asp:Button ID="btnRoleSelect" runat="server" Text="==>" OnClick="btnRoleSelect_Click" /></td>
29关于web.config的读写管理                <td rowspan="2">
30关于web.config的读写管理        <asp:DropDownList ID="DDLRoleTarget" runat="server" Width="100px">
31关于web.config的读写管理        </asp:DropDownList></td>
32关于web.config的读写管理            </tr>
33关于web.config的读写管理            <tr>
34关于web.config的读写管理                <td>
35关于web.config的读写管理        <asp:Button ID="btnRoleDSelect" runat="server" Text="<==" OnClick="btnRoleDSelect_Click" /></td>
36关于web.config的读写管理            </tr>
37关于web.config的读写管理        </table>
38关于web.config的读写管理        <br />
39关于web.config的读写管理    </div>
40关于web.config的读写管理        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="保存" />
41关于web.config的读写管理    </form>

下面是页面的design视图:

关于web.config的读写管理

下面是后台改变系统管理员邮箱配置的相关代码:

 

关于web.config的读写管理关于web.config的读写管理Code
 1关于web.config的读写管理using System;
 2关于web.config的读写管理using System.Data;
 3关于web.config的读写管理using System.Configuration;
 4关于web.config的读写管理using System.Collections;
 5关于web.config的读写管理using System.Web;
 6关于web.config的读写管理using System.Web.Security;
 7关于web.config的读写管理using System.Web.UI;
 8关于web.config的读写管理using System.Web.UI.WebControls;
 9关于web.config的读写管理using System.Web.UI.WebControls.WebParts;
10关于web.config的读写管理using System.Web.UI.HtmlControls;
11关于web.config的读写管理
12关于web.config的读写管理using System.Web.Configuration;
13关于web.config的读写管理
14关于web.config的读写管理using System.Text;
15关于web.config的读写管理
16关于web.config的读写管理namespace APSMMS.SystemConfig
17关于web.config的读写管理关于web.config的读写管理关于web.config的读写管理{
18关于web.config的读写管理    using APSMMSLib.Business;
19关于web.config的读写管理    public partial class SystemConfigPage : System.Web.UI.Page
20关于web.config的读写管理关于web.config的读写管理    关于web.config的读写管理{
21关于web.config的读写管理        Configuration config;
22关于web.config的读写管理        AppSettingsSection appSection;
23关于web.config的读写管理
24关于web.config的读写管理        protected void ConfigInit()
25关于web.config的读写管理关于web.config的读写管理        关于web.config的读写管理{
26关于web.config的读写管理            config = WebConfigurationManager.OpenWebConfiguration("~");
27关于web.config的读写管理            appSection = (AppSettingsSection)config.GetSection("appSettings");
28关于web.config的读写管理        }

29关于web.config的读写管理
30关于web.config的读写管理        protected void Page_Load(object sender, EventArgs e)
31关于web.config的读写管理关于web.config的读写管理        关于web.config的读写管理{
32关于web.config的读写管理            ConfigInit();
33关于web.config的读写管理            if(!IsPostBack)
34关于web.config的读写管理                BindData();
35关于web.config的读写管理        }

36关于web.config的读写管理
37关于web.config的读写管理        protected void BindData()
38关于web.config的读写管理关于web.config的读写管理        关于web.config的读写管理{
39关于web.config的读写管理            tbAdminEmail.Text = appSection.Settings["AdminEmail"].Value;
40关于web.config的读写管理        }

41关于web.config的读写管理
42关于web.config的读写管理       protected void Button1_Click(object sender, EventArgs e)
43关于web.config的读写管理关于web.config的读写管理        关于web.config的读写管理{
44关于web.config的读写管理            appSection.Settings["AdminEmail"].Value = tbAdminEmail.Text.Trim();
45关于web.config的读写管理            config.Save();
46关于web.config的读写管理        }

47关于web.config的读写管理    }

48关于web.config的读写管理}

49关于web.config的读写管理

 

注意:

网站发布以后,注意在web.config文件的权限中添加asp.net相应用户的写权限。
用vss的开发的话,一定要保证web.config签出,才能正确运行。(如没有签出,则不可写)。

 

转载于:https://www.cnblogs.com/Sangplus/archive/2008/08/23/1274675.html