最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2022年05月11日 10:07:40   作者:springsnow  
這篇文章介紹了C#操作配置文件app.config、web.config增刪改的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(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# IEnumerator枚舉器的具體使用

    C# IEnumerator枚舉器的具體使用

    本文主要介紹了C# IEnumerator枚舉器的具體使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • c#獲取季度時間實例代碼(季度的第一天)

    c#獲取季度時間實例代碼(季度的第一天)

    這篇文章主要介紹了c#獲取季度時間:季度的第一天、季度的最后一天等功能,大家參考使用吧
    2013-12-12
  • 基于WPF實現(xiàn)代碼查看器控件

    基于WPF實現(xiàn)代碼查看器控件

    這篇文章主要為大家詳細(xì)介紹了WPF如何實現(xiàn)代碼查看器控件,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)或工作有一定幫助,感興趣的小伙伴可以了解一下
    2022-11-11
  • C#基于DBContext(EF)實現(xiàn)通用增刪改查的REST方法實例

    C#基于DBContext(EF)實現(xiàn)通用增刪改查的REST方法實例

    這篇文章主要介紹了C#基于DBContext(EF)實現(xiàn)通用增刪改查的REST方法實例,是C#程序設(shè)計中非常實用的技巧,需要的朋友可以參考下
    2014-10-10
  • C#圖像重新著色的方法

    C#圖像重新著色的方法

    這篇文章主要介紹了C#圖像重新著色的方法,涉及C#中SetRemapTable方法替換顏色的相關(guān)使用技巧,需要的朋友可以參考下
    2015-06-06
  • c#定期刪除文件的實操方法

    c#定期刪除文件的實操方法

    在本篇文章里小編給大家分享了關(guān)于c#定期刪除文件的方法和步驟,有需要的朋友們可以學(xué)習(xí)下。
    2019-02-02
  • c#如何實現(xiàn)接口事件

    c#如何實現(xiàn)接口事件

    這篇文章主要介紹了c#如何實現(xiàn)接口事件,幫助大家更好的理解和學(xué)習(xí)c#,感興趣的朋友可以了解下
    2020-10-10
  • C#?VB.NET?將Html轉(zhuǎn)為Excel

    C#?VB.NET?將Html轉(zhuǎn)為Excel

    本文介紹通過C#和VB.NET代碼展示將Html轉(zhuǎn)為Excel文檔的方法。文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)C#有一定幫助,感興趣的小伙伴可以了解一下
    2022-03-03
  • 深入解析C#編程中泛型委托的使用

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

    這篇文章主要介紹了C#編程中泛型委托的使用,引用泛型委托的代碼可以指定類型參數(shù)以創(chuàng)建已關(guān)閉的構(gòu)造類型,需要的朋友可以參考下
    2016-02-02
  • C#實現(xiàn)一鍵清空控件值的示例代碼

    C#實現(xiàn)一鍵清空控件值的示例代碼

    這篇文章主要為大家詳細(xì)介紹了如何利用C#語言實現(xiàn)一鍵清空控件值的功能,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)C#有一定幫助,需要的可以參考一下
    2022-09-09

最新評論

建瓯市| 禹州市| 新密市| 濉溪县| 平安县| 翼城县| 铜山县| 扶余县| 晋中市| 临漳县| 留坝县| 年辖:市辖区| 高青县| 白玉县| 德保县| 开封县| 柏乡县| 泾阳县| 江津市| 江阴市| 滦平县| 襄城县| 阿图什市| 宜兰县| 锦屏县| 金湖县| 阳山县| 新闻| 池州市| 河北区| 博白县| 阜康市| 临朐县| 深圳市| 澳门| 中方县| 德州市| 邵东县| 汕头市| 南乐县| 南昌县|