编程语言
首页 > 编程语言> > C# app.config方法

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"); //刷新数据

标签:ConfigurationManager,C#,Text,app,key,AppSettings,config,UpdateAppSettings
来源: https://blog.csdn.net/weixin_44679229/article/details/120557974