欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2022年05月11日 10:07:40   作者:springsnow  
這篇文章介紹了C#操作配置文件app.config、web.config增刪改的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

一、概述

應(yīng)用程序配置文件,對(duì)于asp.net是 web.config,對(duì)于WINFORM程序是 App.Config(ExeName.exe.config)。

配置文件,對(duì)于程序本身來(lái)說(shuō),就是基礎(chǔ)和依據(jù),其本質(zhì)是一個(gè)xml文件,對(duì)于配置文件的操作,從.NET 2.0 開(kāi)始,就非常方便了,提供了 System [.Web] .Configuration 這個(gè)管理功能的命名空間,要使用它,需要添加對(duì) System.configuration.dll的引用。

  • 對(duì)于WINFORM程序,使用 System.Configuration.ConfigurationManager;
  • 對(duì)于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、添加一項(xiàng)

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

4、修改一項(xiàng)

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、刪除一項(xiàng)

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

說(shuō)明:需要注意的是,代碼所修改的并不是app.config,而是[Application_Name].exe.config這個(gè)文件。 
其中Application_Name就是你的可執(zhí)行文件的文件名,而[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.
    }
}

到此這篇關(guān)于C#操作配置文件app.config、web.config增刪改的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • c# JSON返回格式的WEB SERVICE

    c# JSON返回格式的WEB SERVICE

    首先用c#創(chuàng)建一個(gè)web service,主要是利用其WSDL的功能,當(dāng)然也可以利用php創(chuàng)建一個(gè),道理都是一樣的
    2008-12-12
  • Log4Net 日志配置[附帶源碼下載]

    Log4Net 日志配置[附帶源碼下載]

    這篇文章主要介紹了Log4Net 日志配置[附帶源碼下載],需要的朋友可以參考下
    2015-05-05
  • C# 設(shè)計(jì)模式系列教程-原型模式

    C# 設(shè)計(jì)模式系列教程-原型模式

    原型模式隱藏了對(duì)象的創(chuàng)建細(xì)節(jié),對(duì)有些初始化需要占用很多資源的類(lèi)來(lái)說(shuō),對(duì)性能也有很大提高。
    2016-06-06
  • C#面向?qū)ο笤O(shè)計(jì)原則之里氏替換原則

    C#面向?qū)ο笤O(shè)計(jì)原則之里氏替換原則

    這篇文章介紹了C#面向?qū)ο笤O(shè)計(jì)原則之里氏替換原則,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-03-03
  • C#實(shí)現(xiàn)簡(jiǎn)單的登錄界面

    C#實(shí)現(xiàn)簡(jiǎn)單的登錄界面

    我們?cè)谑褂肅#做項(xiàng)目的時(shí)候,基本上都需要制作登錄界面,那么今天我們就來(lái)一步步看看,如果簡(jiǎn)單的實(shí)現(xiàn)登錄界面呢,本文給出2個(gè)例子,由簡(jiǎn)入難,希望大家能夠喜歡。
    2015-11-11
  • C# 中對(duì)象序列化XML的方法

    C# 中對(duì)象序列化XML的方法

    這篇文章主要介紹了C# 中對(duì)象序列化XML的方法,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下
    2021-03-03
  • Winform利用分頁(yè)控件實(shí)現(xiàn)導(dǎo)出PDF文檔功能

    Winform利用分頁(yè)控件實(shí)現(xiàn)導(dǎo)出PDF文檔功能

    當(dāng)前的Winform分頁(yè)控件中,當(dāng)前導(dǎo)出的數(shù)據(jù)一般使用Excel來(lái)處理,但是有框架的使用客戶(hù)希望分頁(yè)控件能夠直接導(dǎo)出PDF,所以本文整理了一下分頁(yè)控件導(dǎo)出PDF的處理過(guò)程,分享一下
    2023-03-03
  • C#實(shí)現(xiàn)給圖片添加日期信息的示例詳解

    C#實(shí)現(xiàn)給圖片添加日期信息的示例詳解

    這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)給圖片添加日期信息,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以了解一下
    2022-12-12
  • C#多維數(shù)組學(xué)習(xí)使用

    C#多維數(shù)組學(xué)習(xí)使用

    當(dāng)下, 由于存儲(chǔ)結(jié)構(gòu)需要, 用多維數(shù)組, 順便學(xué)習(xí)了一下, 將學(xué)習(xí)經(jīng)過(guò)備忘如下
    2012-09-09
  • C# 設(shè)計(jì)模式系列教程-組合模式

    C# 設(shè)計(jì)模式系列教程-組合模式

    組合模式可以使客戶(hù)端調(diào)用簡(jiǎn)單,它可以一致使用組合結(jié)構(gòu)或是其中單個(gè)對(duì)象,簡(jiǎn)化了客戶(hù)端代碼。
    2016-06-06

最新評(píng)論