日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

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

作者:springsnow ? 更新時間: 2022-07-04 編程語言

一、概述

應用程序配置文件,對于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.
    }
}

原文鏈接:https://www.cnblogs.com/springsnow/p/9428724.html

欄目分類
最近更新