关于C# app.config的读写及配置汇总

发布时间:2025-01-30 00:15

在阅读中积累词汇,提升写作表达力 #生活乐趣# #阅读乐趣# #阅读与写作#

@Crazy Snail 已于 2023-04-18 12:37:43 修改

于 2023-02-10 16:59:42 首次发布

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

1.1 基本读写方法及版本变更注意事项

1.读 语句:

String str = ConfigurationManager.AppSettings["DemoKey"];

2.写 语句:

Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

cfa.AppSettings.Settings["DemoKey"].Value = "DemoValue";

cfa.Save();

配置文件内容格式:(app.config文件)

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<add key="DemoKey" value="*" />

</configuration>

System.Configuration.ConfigurationSettings.AppSettings["Key"];

注意:但是现在FrameWork2.0已经明确表示此属性已经过时。并建议改为ConfigurationManager或

WebConfigurationManager。并且AppSettings属性是只读的,并不支持修改属性值.

但是要想调用ConfigurationManager必须要先在工程里添加system.configuration.dll程序集的引用。

(在解决方案管理器中右键点击工程名称,在右键菜单中选择添加引用,.net 标签页下即可找到)

添加引用后可以用 String str = ConfigurationManager.AppSettings["Key"]来获取对应的值了。

更新配置文件:

1.2 引入system.configuration程序集后对应程序更新

str1 = ConfigurationManager.AppSettings["DemoKey"];

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

config.AppSettings.Settings.Clear();

config.AppSettings.Settings.Add("DemoKey", $"{i.ToString()}");

config.Save(ConfigurationSaveMode.Modified);

ConfigurationManager.RefreshSection("appSettings");

str2 = ConfigurationManager.AppSettings["DemoKey"];

1.3 app.config 文件及对应代码部分

app.config文件:

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<startup>

<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />

</startup>

<appSettings>

<add key="RecipeName1" value="recipeName1" ></add>

<add key="RecipeName2" value="recipeName2" ></add>

<add key="RecipeName3" value="recipeName3" ></add>

<add key="RecipeName4" value="recipeName4" ></add>

<add key="RecipeName5" value="recipeName5" ></add>

<add key="RecipeName6" value="recipeName6" ></add>

</appSettings>

</configuration>

代码部分:

private void Button_Click_1(object sender, RoutedEventArgs e)

{

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

config.AppSettings.Settings.Remove($"RecipeName2");

config.AppSettings.Settings.Add($"RecipeName2", $"456456");

config.AppSettings.Settings.Add($"RecipeName2", $"456456");

config.AppSettings.Settings.Remove($"RecipeName3");

config.AppSettings.Settings.Add($"RecipeName3", $"3333333");

config.Save(ConfigurationSaveMode.Modified);

ConfigurationManager.RefreshSection("appSettings");

string sh1 = ConfigurationManager.AppSettings["RecipeName1"];

string sh2 = ConfigurationManager.AppSettings["RecipeName2"];

string sh3 = ConfigurationManager.AppSettings["RecipeName3"];

}

网址:关于C# app.config的读写及配置汇总 https://www.yuejiaxmz.com/news/view/746805

相关内容

关于健康饮食的倡议书汇总
关于打扫的名言汇总
关于电脑日常维护小常识汇总
高考作文模拟写作:“关于提升幸福感”导写及范文.docx
基于 flask 框架的模拟instagram 图片分享网站的开发 4
重磅!2018国家环保产业最新政策汇总及解读
电脑如何清理c盘垃圾?有效清理C盘的多种方法,超全汇总
关于慢节奏生活的作文(汇总10篇)
幸福的哲学读书笔记汇总
关于亲子活动方案汇总7篇

随便看看