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方法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程