• config


    https://www.cnblogs.com/dayang12525/p/7866766.html

    https://www.cnblogs.com/davytitan/p/4081573.html

    https://www.cnblogs.com/tuyile006/archive/2008/06/22/1227667.html

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
      </startup>
      <appSettings>
        <add key="Setting1" value="May 5, 2014"/>
        <add key="Setting2" value="May 6, 2014"/>
      </appSettings>
    </configuration>
    View Code
        class Program
        {
            static void Main(string[] args)
            {
                ReadAllSettings();
                ReadSetting("Setting1");
                ReadSetting("NotValid");
                AddUpdateAppSettings("NewSetting", "May 7, 2014");
                AddUpdateAppSettings("Setting1", "May 8, 2014");
                ReadAllSettings();
                Console.ReadLine();
            }
    
            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");
                }
            }
        }
    View Code
            public static void SetValue(string key, string value)
            {
                System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
                xDoc.Load(HttpContext.Current.Server.MapPath("~/Appssa.config"));
                System.Xml.XmlNode xNode;
                System.Xml.XmlElement xElem1;
                System.Xml.XmlElement xElem2;
                xNode = xDoc.SelectSingleNode("//appSettings");
    
                xElem1 = (System.Xml.XmlElement)xNode.SelectSingleNode("//add[@key='" + key + "']");
                if (xElem1 != null) xElem1.SetAttribute("value", value);
                else
                {
                    xElem2 = xDoc.CreateElement("add");
                    xElem2.SetAttribute("key", key);
                    xElem2.SetAttribute("value", value);
                    xNode.AppendChild(xElem2);
                }
                xDoc.Save(HttpContext.Current.Server.MapPath("~/Apps.config"));
            }
    View Code
  • 相关阅读:
    Spring Boot
    java 解析命令行参数
    idea 打包java程序
    Dynomite 安装配置
    python 装饰器
    IntelliJ IDEA教程
    ubuntu14.04 rabbitmq安装与使用 --修改RabbitMQ数据存储位置
    ribbbitMq 教程,详细
    spring 的 切片Aspect 最常用记录方法执行时间
    Spring-data-jpa 常用的时间注解
  • 原文地址:https://www.cnblogs.com/futengsheng/p/7966501.html
Copyright © 2020-2023  润新知