夜间模式暗黑模式
字体
阴影
滤镜
圆角
主题色
C# 应用配置操作(*.Config)

添加引用 using System.Configuration;

class Config
    {
        //没弄明白appSettings和connectionStrings除了在配置文件中节点不同以外还有什么区别,我更倾向于使用appSettings

        static public void Write(string key,string value)
        {
            bool Exist = false;
            var conf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            foreach (string s in conf.AppSettings.Settings.AllKeys)
            {
                if (s == key)
                {
                    conf.AppSettings.Settings[s].Value = value;
                    Exist = true;
                }
            }
            if (!Exist)
            {
                conf.AppSettings.Settings.Add(key, value);
            }
            conf.Save();
            ConfigurationManager.RefreshSection("appSettings");
        }

        static public string Read(string key)
        {
            ConfigurationManager.RefreshSection("appSettings");
            var conf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            return conf.AppSettings.Settings[key].Value ?? "";
        }

        static public void Remove(string key)
        {
            var conf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            conf.AppSettings.Settings.Remove(key);
            ConfigurationManager.RefreshSection("appSettings");
        }
    }

暂无评论

发送评论 编辑评论


				
上一篇
下一篇