亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

一、概述

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

配置文件,對于程序本身來說,就是基礎(chǔ)和依據(jù),其本質(zhì)是一個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就是你的可執(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增刪改的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • c# JSON返回格式的WEB SERVICE

    c# JSON返回格式的WEB SERVICE

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

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

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

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

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

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

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

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

    我們在使用C#做項目的時候,基本上都需要制作登錄界面,那么今天我們就來一步步看看,如果簡單的實現(xiàn)登錄界面呢,本文給出2個例子,由簡入難,希望大家能夠喜歡。
    2015-11-11
  • C# 中對象序列化XML的方法

    C# 中對象序列化XML的方法

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

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

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

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

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

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

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

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

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

最新評論