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

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

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

 

 

 

转载地址:http://rnhzx.baihongyu.com/

你可能感兴趣的文章
mac下使用Intellij、adt-studio、Appcode,推荐使用Apple Jdk6
查看>>
Maven学习总结(十)——使用Maven编译项目gbk的不可映射问题
查看>>
Spring学习总结(2)——Spring的常用注解
查看>>
php5编译安装常见错误和解决办法集锦
查看>>
Linux远程访问及控制
查看>>
Oracle中如何删除某个用户下的所有数据呢
查看>>
MongoDB实战系列之五:mongodb的分片配置
查看>>
Unable to determine local host from URL REPOSITORY_URL=http://
查看>>
Java Tomcat SSL 服务端/客户端双向认证(二)
查看>>
java基础(1)
查看>>
ORACLE配置,修改tnsnames.ora文件实例
查看>>
用户无法在输入框中键入数字
查看>>
Workstation服务无法启动导致无法访问文件服务器
查看>>
Gradle:Basic Project
查看>>
.Net组件程序设计之远程调用(二)
查看>>
ant中文教程
查看>>
Linux常用命令(一)
查看>>
安装和使用 Elasticsearch
查看>>
WSUS数据库远端存储条件下切换域及数据库迁移
查看>>
JSON总结
查看>>