zoukankan      html  css  js  c++  java
  • ConfigurationManager 读写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");
                }
            }
        }
    }

    前面的示例假定您的项目有一个 App.config 文件如下所示。

     
    <?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>
  • 相关阅读:
    AMD平台如何使用Android Studio官方的高性能模拟器
    Nginx安装SSL证书,开启HTTPS加密
    【English】20190429
    【Teradata】TD Unicode编码格式下varchar定义测试
    【Teradata TTU】Windows TTU安装工具列表
    【English EMail】2019 Q2 Public Holiday Announcement
    【English】20190428
    【张东武 老架一路74式第一段】第二式 金刚捣碓
    【影音制作】编辑视频
    【Teradata SQL】多行转一列函数TDStats.udfConcat
  • 原文地址:https://www.cnblogs.com/marblemm/p/9329297.html
Copyright © 2011-2022 走看看