ASP.NET 中config配置文件的读写操作

本文介绍了在ASP.NET中如何使用ConfigurationManager和WebConfigurationManager进行config配置文件的读写操作。内容包括普通配置节点、数据源配置节点和自定义配置节点的读取与修改,详细展示了在不同场景下的应用实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

现在FrameWork2.0以上使用的是:ConfigurationManager或WebConfigurationManager。并且AppSettings属性是只读的,并不支持修改属性值

一、使用ConfigurationManager:

1.首先添加引用-System.configguration

2.引用命名空间

3.config配置文件配置节点

常用的分为三种:普通配置节点,数据源配置节点以及自定义配置节点;

1>.普通配置节点:

    <appSettings>
      <add key="COM1" value="COM1,9600,8,None,1,已启用" />
    </appSettings>

2>.数据源配置节点:

<connectionStrings>
  <add name="sqlDB" connectionString="server=.;database=DATA_day01;user=sa;pwd=123456"/>
</connectionStrings>

3>.自定义配置节点:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
  
  <appSettings>
    <add key="key1" value="Welcome to China"/>
  </appSettings>

  <connectionStrings>
    <add name="sqlDB"             
      connectionString="server=.;database=DATA_day01;user=sa;pwd=123456"/>
  </connectionStrings>
  
</configuration>

实际应用:

1.获取配置节点的值:

button1 点击获取配置节<appSettings>指定key的value值

button2 点击获取配置节<connectionStrings>指定name的connectionString值

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //获取配置节点<appSettings>指定key的value值
            textBox1.Text = ConfigurationManager.AppSettings["key1"];
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //点击获取配置节点<connectionStrings>指定name的connectionString值
            textBox2.Text = ConfigurationManager.ConnectionStrings["sqlDB"].ToString();
        }
    }
}

效果预览:

2.修改配置节点的值

获取值按钮 点击获取配置节<appSettings>指定key的value值

修改文本按钮 点击修改配置节<appSettings>指定key的value值为文本框的值

获取修改后值按钮 点击获取配置节<appSettings>指定key新的value值

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //获取配置节点<appSettings>指定key的value值
            textBox1.Text = ConfigurationManager.AppSettings["key1"];
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //修改配置节点<appSettings>指定key的value值为文本框的值
            ConfigurationManager.AppSettings.Set("key1", textBox2.Text);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //获取配置节点<appSettings>指定key新的value值
            textBox3.Text = ConfigurationManager.AppSettings["key1"];
        }
    }
}

效果预览:

二、使用WebConfigurationManager:

1.首先同样是添加引用:

Web.config里的配置节点:

  <appSettings>
    <add key="FilePath" value="D:\Test\WebConfigManager\Upload\" />
    <add key="AA" value="D:\" />
  </appSettings>

1>.读取

    string filepath = ConfigurationManager.AppSettings["FilePath"];

2>.添加 

        //WebConfigManager是web.config所在的文件夹名
        Configuration config = WebConfigurationManager.OpenWebConfiguration("/WebConfigManager");
        AppSettingsSection app = config.AppSettings;
        app.Settings.Add("AA", "D:\\");
        config.Save(ConfigurationSaveMode.Modified);

3>.修改

        //WebConfigManager是web.config所在的文件夹名
          Configuration config = WebConfigurationManager.OpenWebConfiguration("/WebConfigManager");
            AppSettingsSection app = config.AppSettings;
            app.Settings["AA"].Value = @"D:\";
            config.Save(ConfigurationSaveMode.Modified);

4>.删除

//WebConfigManager是web.config所在的文件夹名
Configuration config = WebConfigurationManager.OpenWebConfiguration("/WebConfigManager");
        AppSettingsSection app = config.AppSettings;
        app.Settings.Remove("AA");
        config.Save(ConfigurationSaveMode.Modified);

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值