C# app.config方法
2021/9/30 1:12:25
本文主要是介绍C# app.config方法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
c#方法
//appconfig方法 #region config操作 private static Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); public static string GetAppSetting(string key) { if (ConfigurationManager.AppSettings.AllKeys.Contains(key)) { string value = config.AppSettings.Settings[key].Value; return value; } else { return null; } } public static void UpdateAppSettings(string key, string value) { if (ConfigurationManager.AppSettings.AllKeys.Contains(key)) { //如果当前节点存在,则更新当前节点 config.AppSettings.Settings[key].Value = value; config.Save(ConfigurationSaveMode.Modified); } else { Console.WriteLine("当前节点不存在"); } } #endregion
appconfig配置
<appSettings> <clear /> <add key="Organization" value="宁波诗兰姆汽车零部件有限公司" /> <add key="TimeRefresh" value="00:00:01" /> <add key="TableDay1" value="61" /> <add key="TableDay2" value="90" /> <add key="TableDay3" value="91" /> <add key="TableDay4" value="180" /> <add key="TableMonth" value="6" /> <add key="LineYear" value="2020" /> <add key="LineMonth" value="1" /> <add key="BarYear" value="2020" /> <add key="BarMonth" value="10" /> <add key="BarDay" value="1" /> </appSettings>
取值
//Organization OrganizationName.Text = ConfigurationManager.AppSettings["Organization"]; //basic setting TimeRefresh.Text = ConfigurationManager.AppSettings["TimeRefresh"]; //table TableDay1.Text = ConfigurationManager.AppSettings["TableDay1"]; TableDay2.Text = ConfigurationManager.AppSettings["TableDay2"]; TableDay3.Text = ConfigurationManager.AppSettings["TableDay3"]; TableDay4.Text = ConfigurationManager.AppSettings["TableDay4"]; TableMonth.Text = ConfigurationManager.AppSettings["TableMonth"]; //line LineYear.Text = ConfigurationManager.AppSettings["LineYear"]; LineMonth.Text = ConfigurationManager.AppSettings["LineMonth"]; //bar BarYear.Text = ConfigurationManager.AppSettings["BarYear"]; BarMonth.Text = ConfigurationManager.AppSettings["BarMonth"]; BarDay.Text = ConfigurationManager.AppSettings["BarDay"];
保存
//Organization UpdateAppSettings("Organization", OrganizationName.Text); //basic setting UpdateAppSettings("TimeRefresh", TimeRefresh.Text); //table UpdateAppSettings("TableDay1", TableDay1.Text); UpdateAppSettings("TableDay2", TableDay2.Text); UpdateAppSettings("TableDay3", TableDay3.Text); UpdateAppSettings("TableDay4", TableDay4.Text); UpdateAppSettings("TableMonth", TableMonth.Text); //line UpdateAppSettings("LineYear", LineYear.Text); UpdateAppSettings("LineMonth", LineMonth.Text); //bar UpdateAppSettings("BarYear", BarYear.Text); UpdateAppSettings("BarMonth", BarMonth.Text); UpdateAppSettings("BarDay", BarDay.Text);
刷新
ConfigurationManager.RefreshSection("appSettings"); //刷新数据
这篇关于C# app.config方法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-11-18微软研究:RAG系统的四个层次提升理解与回答能力
- 2024-11-15C#中怎么从PEM格式的证书中提取公钥?-icode9专业技术文章分享
- 2024-11-14云架构设计——如何用diagrams.net绘制专业的AWS架构图?
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#
- 2024-01-24Advanced .Net Debugging 1:你必须知道的调试工具