博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ConfigurationManager.AppSettings Property
阅读量:5043 次
发布时间:2019-06-12

本文共 3314 字,大约阅读时间需要 11 分钟。

在app.config文件中添加如下配置

 

 

访问方式(需要添加System.Configuration.dll的引用)

string server = ConfigurationManager.AppSettings["Server"];  int port = Convert.ToInt32(ConfigurationManager.AppSettings["Port"]);

 

Gets the  data for the current application's default configuration.

Namespace:   

Assembly:  System.Configuration (in System.Configuration.dll)

public static NameValueCollection AppSettings { get; }

Property Value

Type: 

Returns a  object that contains the contents of the  object for the current application's default configuration.

Exceptions

Could not retrieve a  object with the application settings data.

Remarks

A  object contains the contents of the configuration file's appSettings section.

The first example shows a simple console application that reads application settings, adds a new setting, and updates an existing setting.

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");            }        }    }}

 

 

 

转载于:https://www.cnblogs.com/chucklu/p/5280043.html

你可能感兴趣的文章
Java8 Lambda表达应用 -- 单线程游戏server+异步数据库操作
查看>>
次序+“选择不重复的记录”(3)——最大记录
查看>>
Codeforces 450 C. Jzzhu and Chocolate
查看>>
[Unity3D]Unity3D游戏开发MatchTarget的作用攀登效果实现
查看>>
ACdream 1115 Salmon And Cat (找规律&&打表)
查看>>
JSON、JSONP、Ajax的区别
查看>>
AngularJS学习篇(一)
查看>>
【转载】 IP实时传输协议RTP/RTCP详解
查看>>
关于Xshell无法连接centos6.4的问题
查看>>
Linux系统的数据写入机制--延迟写入
查看>>
css3动画——基本准则
查看>>
javaweb常识
查看>>
Java注解
查看>>
时间>金钱
查看>>
元数据元素
查看>>
Visual Studio Code 构建C/C++开发环境
查看>>
web自己主动保存表单
查看>>
一个小的日常实践——高速Fibonacci数算法
查看>>
创建与删除索引
查看>>
java的基本数据类型
查看>>