最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#之App.Config文件操作的實現(xiàn)

 更新時間:2026年01月04日 10:03:30   作者:*Major*-莙工科技有限公司  
AppConfigHelper提供了一種方便的方式來讀取、添加、修改和刪除應(yīng)用程序配置文件中的配置項,本文主要介紹了C#之App.Config文件操作的實現(xiàn),感興趣的可以了解一下

一 AppConfigHelper

  using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Helper
{
    /// <summary>
    /// 操作配置文件(App.config)
    /// </summary>
    public class AppConfigHelper
    {

        /// <summary>
        /// 輸入Key的值,返回配置的值
        /// </summary>
        /// <param name="KeyName"></param>
        /// <returns></returns>
        public string ReadConfig(string KeyName)
        {
            Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            return cfa.AppSettings.Settings[KeyName].Value;
        }

        /// <summary>
        /// 根據(jù)配置的名稱,查詢獨立的數(shù)據(jù),讀取app.config
        /// </summary>
        /// <param name="ProName"></param>
        /// <param name="KeyName"></param>
        /// <returns></returns>
        public string ReadConfig(string ProName, string KeyName)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            var mySection = config.GetSection(ProName) as CommonSection;
            foreach (CommonSection.CommonKeyValueSetting add in mySection.KeyValues)
            {
                if (add.Key == KeyName)
                {
                    return add.Value;
                }
            }
            return null;
        }

        /// <summary>
        /// 增加配置文件
        /// </summary>
        /// <param name="KeyName"></param>
        /// <param name="Value"></param>
        public void AddConfig(string KeyName, string Value)
        {
            Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            cfa.AppSettings.Settings.Add(KeyName, Value);
            cfa.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
        }

        /// <summary>
        /// 根據(jù)配置的名稱,查詢獨立的數(shù)據(jù),并添加獨立的內(nèi)容
        /// </summary>
        /// <param name="ProName"></param>
        /// <param name="KeyName"></param>
        /// <param name="Value"></param>
        /// <returns></returns>
        public void AddConfig(string ProName, string KeyName, string Value)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            var mySection = config.GetSection(ProName) as CommonSection;
            mySection.KeyValues.Add(new CommonSection.CommonKeyValueSetting() { Key = KeyName, Value = Value });
            config.Save();
            ConfigurationManager.RefreshSection(ProName);  //刷新
        }

        /// <summary>
        /// 刪除配置文件
        /// </summary>
        /// <param name="KeyName"></param>
        public void DeleteConfig(string KeyName)
        {
            Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            cfa.AppSettings.Settings.Remove(KeyName);
            cfa.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
        }

        /// <summary>
        /// 修改配置文件數(shù)據(jù)
        /// </summary>
        /// <param name="KeyName"></param>
        /// <param name="Value"></param>
        public void WriteConfig(string KeyName, string Value)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            config.AppSettings.Settings[KeyName].Value = Value;
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
        }

        /// <summary>
        /// 根據(jù)配置的名稱,查詢獨立的數(shù)據(jù),并修改內(nèi)容
        /// </summary>
        /// <param name="ProName"></param>
        /// <param name="KeyName"></param>
        /// <param name="Value"></param>
        /// <returns></returns>
        public void WriteConfig(string ProName, string KeyName, string Value)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            var mySection = config.GetSection(ProName) as CommonSection;
            mySection.KeyValues.Remove(KeyName);
            mySection.KeyValues.Add(new CommonSection.CommonKeyValueSetting() { Key = KeyName, Value = Value });
            config.Save();
            ConfigurationManager.RefreshSection(ProName);  //刷新
        }

    }
    /// <summary>
    /// 配置節(jié)點基類
    /// </summary>
    public class CommonSection : ConfigurationSection
    {

        private static ConfigurationProperty s_property =
            new ConfigurationProperty(string.Empty, typeof(CommonKeyValueCollection), null, ConfigurationPropertyOptions.IsDefaultCollection);

        [ConfigurationProperty("", Options = ConfigurationPropertyOptions.IsDefaultCollection)]
        public CommonKeyValueCollection KeyValues
        {
            get
            {
                return (CommonKeyValueCollection)base[s_property];
            }
        }

        /// <summary>
        /// 自定義一個集合
        /// </summary>
        [ConfigurationCollection(typeof(CommonKeyValueSetting))]
        public class CommonKeyValueCollection : ConfigurationElementCollection
        {

            public CommonKeyValueCollection() : base(StringComparer.OrdinalIgnoreCase)
            {

            }

            new public CommonKeyValueSetting this[string name]
            {
                get { return (CommonKeyValueSetting)base.BaseGet(name); }
                set { base[name] = value; }
            }

            protected override ConfigurationElement CreateNewElement()
            {
                return new CommonKeyValueSetting();
            }


            protected override object GetElementKey(ConfigurationElement element)
            {
                return ((CommonKeyValueSetting)element).Key;
            }

            public void Add(CommonKeyValueSetting setting)
            {
                this.BaseAdd(setting);
            }

            public void Clear()
            {
                base.BaseClear();
            }

            public void Remove(string name)
            {
                base.BaseRemove(name);
            }

        }

        /// <summary>
        /// 集合中的每個元素
        /// </summary>
        public class CommonKeyValueSetting : ConfigurationElement
        {

            /// <summary>
            /// 鍵
            /// </summary>
            [ConfigurationProperty("key", IsRequired = true)]
            public string Key
            {
                get { return this["key"].ToString(); }
                set { this["key"] = value; }
            }

            /// <summary>
            /// 值
            /// </summary>
            [ConfigurationProperty("value", IsRequired = true)]
            public string Value
            {
                get { return this["value"].ToString(); }
                set { this["value"] = value; }
            }

        }

    }

}

二 讀取標準 appSettings

添加appSettings

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
    </startup>

	<appSettings>
		<add key="Width" value="100"/>
		<add key="Height" value="200"/>
		<add key="Area" value="99"/>
	</appSettings>
</configuration>

讀取appSettings

  // 全局變量
  AppConfigHelper config = new AppConfigHelper();
  string h = config.ReadConfig("Height");
  MessageBox.Show(h.ToString());

三 添加配置 appSettings

   config.AddConfig("XXXX", "vvvv");
   MessageBox.Show("添加成功?。?);

   string va = config.ReadConfig("XXXX");

四 修改標準 appSettings

         config.WriteConfig("Height", "7777");
         MessageBox.Show("修改成功?。?);

五 刪除配置 appSettings

       config.DeleteConfig("Height");
       MessageBox.Show("刪除成功?。?);

到此這篇關(guān)于C#之App.Config文件操作的實現(xiàn)的文章就介紹到這了,更多相關(guān)C# App.Config文件操作內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

祁东县| 资源县| 涿鹿县| 政和县| 年辖:市辖区| 谷城县| 潍坊市| 县级市| 汨罗市| 云和县| 繁昌县| 都安| 曲水县| 象州县| 如东县| 中卫市| 深圳市| 扎鲁特旗| 同德县| 舞钢市| 阿拉善左旗| 滦南县| 辉县市| 错那县| 金华市| 石河子市| 河南省| 鹤庆县| 乌鲁木齐县| 彰化市| 安义县| 焦作市| 修水县| 慈利县| 衡阳县| 重庆市| 定襄县| 辽源市| 凉城县| 房产| 通化市|