zoukankan      html  css  js  c++  java
  • 读写app.config AppSettings,保留注释与不保留注释

    不保留

    using System;
    using System.Configuration;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                ReadAllSettings();
                ReadSetting("Setting1");
                ReadSetting("NotValid");
                AddUpdateAppSettings("NewSetting", "May 7, 2014");
                AddUpdateAppSettings("Setting1", "May 8, 2014");
                ReadAllSettings();
            }
    
            static void ReadAllSettings()
            {
                try
                {
                    var appSettings = ConfigurationManager.AppSettings;
    
                    if (appSettings.Count == 0)
                    {
                        Console.WriteLine("AppSettings is empty.");
                    }
                    else
                    {
                        foreach (var key in appSettings.AllKeys)
                        {
                            Console.WriteLine("Key: {0} Value: {1}", key, appSettings[key]);
                        }
                    }
                }
                catch (ConfigurationErrorsException)
                {
                    Console.WriteLine("Error reading app settings");
                }
            }
    
            static void ReadSetting(string key)
            {
                try
                {
                    var appSettings = ConfigurationManager.AppSettings;
                    string result = appSettings[key] ?? "Not Found";
                    Console.WriteLine(result);
                }
                catch (ConfigurationErrorsException)
                {
                    Console.WriteLine("Error reading app settings");
                }
            }
    
            static void AddUpdateAppSettings(string key, string value)
            {
                try
                {
                    var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                    var settings = configFile.AppSettings.Settings;
                    if (settings[key] == null)
                    {
                        settings.Add(key, value);
                    }
                    else
                    {
                        settings[key].Value = value;
                    }
                    configFile.Save(ConfigurationSaveMode.Modified);
                    ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name);
                }
                catch (ConfigurationErrorsException)
                {
                    Console.WriteLine("Error writing app settings");
                }
            }
        }
    }

    不删注释:

    public static void SaveAppSettingsMethod2(string key, string value)
            {
                //验证key value
                //To Do
    
                XmlDocument xml = new XmlDocument();
          string configPath = Application.ExecutablePath + ".config"; xml.Load(configPath); XmlNodeList nodeList
    = xml.GetElementsByTagName("appSettings"); if (nodeList != null) { if (nodeList.Count >= 1) { XmlNode node = nodeList[0]; foreach (XmlNode item in node) { if (item.NodeType == XmlNodeType.Comment) { continue; } XmlAttribute xaKey = item.Attributes["key"]; XmlAttribute xaValue = item.Attributes["value"]; if (xaKey != null && xaValue != null && xaKey.Value.Equals(key)) { xaValue.Value = value; } } } } xml.Save(configPath); }
  • 相关阅读:
    安装虚拟环境virtualenv
    安装python3、ipython、jupyter
    配置yum源
    面向对象
    sqrt开平方算法的尝试,是的看了卡马克大叔的代码,我来试试用C#写个0x5f3759df和0x5f375a86跟System.Math.Sqrt到底哪个更强
    python开发环境
    phthon中的open函数模式
    picoscope 动态链接库调用位置确定,可进行图标编辑
    设计模式笔记(2)-工厂模式
    设计模式笔记(1)-单体模式
  • 原文地址:https://www.cnblogs.com/gaocong/p/5505210.html
Copyright © 2011-2022 走看看