C#操作配置文件app.config、web.config增删改

 更新时间:2022年05月11日 10:07:40   作者:springsnow  
这篇文章介绍了C#操作配置文件app.config、web.config增删改的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

一、概述

应用程序配置文件,对于asp.net是 web.config,对于WINFORM程序是 App.Config(ExeName.exe.config)。

配置文件,对于程序本身来说,就是基础和依据,其本质是一个xml文件,对于配置文件的操作,从.NET 2.0 开始,就非常方便了,提供了 System [.Web] .Configuration 这个管理功能的命名空间,要使用它,需要添加对 System.configuration.dll的引用。

  • 对于WINFORM程序,使用 System.Configuration.ConfigurationManager;
  • 对于ASP.NET 程序, 使用 System.Web.Configuration.WebConfigurationManager;

二、配置文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="y" value="this is Y"/>
  </appSettings>
</configuration>

三、读取配置文件:

1. 读取值:

System.Configuration.ConfigurationManager.AppSettings[“y”];

Asp.Net程序:

System.Web.Configuration.WebConfigurationManager.AppSettings[“y”];

读取最新值:

Configuration config =  ConfigurationManager.OpenExeConfiguration(null);
AppSettingsSection app = config.AppSettings;
// 或者AppSettingsSection app =config.GetSection("AppSettings") as AppSettingsSection

Asp.Net程序读取值:

Configuration config = WebConfigurationManager.OpenWebConfiguration(null);
AppSettingsSection app = config.AppSettings;
// 或者AppSettingsSection app =config.GetSection("AppSettings") as AppSettingsSection;

2、查看值

    string y=app.Settings["y"].Value;
    foreach (string key in app.Settings)
    {
        Console.WriteLine(key+","+ app.Settings[key].Value);
    }

3、添加一项

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
AppSettingsSection app = config.AppSettings;
app.Settings.Add("x", "this is X");
config.Save(ConfigurationSaveMode.Modified);

4、修改一项

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
AppSettingsSection app = config.AppSettings;
//app.Settings.Add("x", "this is X");
app.Settings["x"].Value = "this is not Y";
config.Save(ConfigurationSaveMode.Modified);

5、删除一项

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
AppSettingsSection app = config.AppSettings;
app.Settings.Remove("x");
config.Save(ConfigurationSaveMode.Modified);

说明:需要注意的是,代码所修改的并不是app.config,而是[Application_Name].exe.config这个文件。 
其中Application_Name就是你的可执行文件的文件名,而[Application_Name].exe.config才是真正起作用的配置文件。 
至于app.config,把它理解为是初始化配置文件比较合适。

四、连接字符串

1、读取连接字符串

ConnectionStringSettings conn=ConfigurationManager.ConnectionStrings["y"];

或者

Configuration config = ConfigurationManager.OpenExeConfiguration(null);
ConnectionStringsSection connSeciton = config.ConnectionStrings;
connSeciton.ConnectionStrings.Add(new ConnectionStringSettings("name", "connectionstring", "provider"));

2、加密字符串

public static void EncryptConnectionString(bool encrypt)
{
    Configuration configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    ConnectionStringsSection configSection = configFile.GetSection("connectionStrings") as ConnectionStringsSection;
    if ((!(configSection.ElementInformation.IsLocked)) && (!(configSection.SectionInformation.IsLocked)))
    {
        if (encrypt && !configSection.SectionInformation.IsProtected)       //encrypt is false to unencrypt
        {
            configSection.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
        }
        if (!encrypt && configSection.SectionInformation.IsProtected)
        {
            configSection.SectionInformation.UnprotectSection();    //encrypt is true so encrypt
        }
        configSection.SectionInformation.ForceSave = true;  //re-save the configuration file section
        configFile.Save();  // Save the current configuration.
    }
}

到此这篇关于C#操作配置文件app.config、web.config增删改的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • C# IEnumerator枚举器的具体使用

    C# IEnumerator枚举器的具体使用

    本文主要介绍了C# IEnumerator枚举器的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-06-06
  • c#获取季度时间实例代码(季度的第一天)

    c#获取季度时间实例代码(季度的第一天)

    这篇文章主要介绍了c#获取季度时间:季度的第一天、季度的最后一天等功能,大家参考使用吧
    2013-12-12
  • 基于WPF实现代码查看器控件

    基于WPF实现代码查看器控件

    这篇文章主要为大家详细介绍了WPF如何实现代码查看器控件,文中的示例代码讲解详细,对我们学习或工作有一定帮助,感兴趣的小伙伴可以了解一下
    2022-11-11
  • C#基于DBContext(EF)实现通用增删改查的REST方法实例

    C#基于DBContext(EF)实现通用增删改查的REST方法实例

    这篇文章主要介绍了C#基于DBContext(EF)实现通用增删改查的REST方法实例,是C#程序设计中非常实用的技巧,需要的朋友可以参考下
    2014-10-10
  • C#图像重新着色的方法

    C#图像重新着色的方法

    这篇文章主要介绍了C#图像重新着色的方法,涉及C#中SetRemapTable方法替换颜色的相关使用技巧,需要的朋友可以参考下
    2015-06-06
  • c#定期删除文件的实操方法

    c#定期删除文件的实操方法

    在本篇文章里小编给大家分享了关于c#定期删除文件的方法和步骤,有需要的朋友们可以学习下。
    2019-02-02
  • c#如何实现接口事件

    c#如何实现接口事件

    这篇文章主要介绍了c#如何实现接口事件,帮助大家更好的理解和学习c#,感兴趣的朋友可以了解下
    2020-10-10
  • C# VB.NET 将Html转为Excel

    C# VB.NET 将Html转为Excel

    本文介绍通过C#和VB.NET代码展示将Html转为Excel文档的方法。文中的示例代码讲解详细,对我们学习C#有一定帮助,感兴趣的小伙伴可以了解一下
    2022-03-03
  • 深入解析C#编程中泛型委托的使用

    深入解析C#编程中泛型委托的使用

    这篇文章主要介绍了C#编程中泛型委托的使用,引用泛型委托的代码可以指定类型参数以创建已关闭的构造类型,需要的朋友可以参考下
    2016-02-02
  • C#实现一键清空控件值的示例代码

    C#实现一键清空控件值的示例代码

    这篇文章主要为大家详细介绍了如何利用C#语言实现一键清空控件值的功能,文中的示例代码讲解详细,对我们学习C#有一定帮助,需要的可以参考一下
    2022-09-09

最新评论