• 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
  • 相关阅读:
    C/S 随思录
    3dTiles 最后一块拼图:几何误差与屏幕空间误差
    aps.net cannot connect to runtime process
    asp.net web api swagger使用总结
    asp.net webaip 跨域
    LightGBM算法实践
    Zabbix 机器 CPU 飙高 和 时区相差8个小时
    【vue踩坑记录】3、“Error in render: "TypeError: Cannot read property '0' of undefined"”渲染错误问题
    1-关于补码的理解
    vscode快捷键
  • 原文地址:https://www.cnblogs.com/futengsheng/p/7966501.html
Copyright © 2020-2023  润新知