ConfigurationManager.AppSettings Property
在app.config文件中添加如下配置
<appSettings>
<add key="Server" value="127.0.0.1"/>
<add key="Port" value=""/>
</appSettings>
访问方式(需要添加System.Configuration.dll的引用)
string server = ConfigurationManager.AppSettings["Server"];
int port = Convert.ToInt32(ConfigurationManager.AppSettings["Port"]);
Gets the AppSettingsSection data for the current application's default configuration.
Namespace: System.Configuration
Assembly: System.Configuration (in System.Configuration.dll)
public static NameValueCollection AppSettings { get; }
Property Value
Type: System.Collections.Specialized.NameValueCollection
Returns a NameValueCollection object that contains the contents of the AppSettingsSection object for the current application's default configuration.
Exceptions
| ConfigurationErrorsException |
Could not retrieve a NameValueCollection object with the application settings data. |
Remarks
A AppSettingsSection 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 == )
{
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");
}
}
}
}
<?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>
How to find path of active app.config file?
Try this
AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
Hope it helps
ConfigurationManager.AppSettings Property的更多相关文章
- 点点滴滴-ConfigurationManager.AppSettings
在写程序的配置文件,里面添加了几个配置,下面是appSettings节点的设置 <appSettings> <add key="StyleFolder" valu ...
- ConfigurationManager.AppSettings方法
一 配置文件概述: 应用程序配置文件是标准的 XML 文件,XML 标记和属性是区分大小写的.它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而不必重编译应用程序.配置文件的根节点是conf ...
- ConfigurationManager.AppSettings 属性 appSettings
https://msdn.microsoft.com/zh-cn/library/system.configuration.configurationmanager.appsettings(v=vs. ...
- Asp.Net Core 中无法使用 ConfigurationManager.AppSettings
刚刚接触.net core ,准备把之前的一些技术常用工具先移植到.net Standard上面来, 方便以后使用,结果用到ConfigurationManager 的 AppSettings 就出现 ...
- ConfigurationManager 读写AppSettings键值对
using System; using System.Configuration; namespace ConsoleApplication1 { class Program { static voi ...
- System.ConfigurationManager类用于对配置文件的读取
http://blog.csdn.net/ligenyingsr/article/details/54095986 System.ConfigurationManager类用于对配置文件的读取.其具有 ...
- 在Asp.Net MVC 中如何用JS访问Web.Config中appSettings的值
应用场景: 很多时候我们要在Web.Config中添加appSettings的键值对来标识一些全局的信息,比如:调用service的domain,跳转其他网站页面的url 等等: 那么此时就涉及到了一 ...
- AppSettings和connectionStrings的却别(转)
AppSettings是ASP.NET1.1时期用的,在.NET Framework 2.0中,新增了ConnectionStrings. 1.<connectionStrings> &l ...
- <connectionStrings> <appSettings> 读取方法
C#中ConnectionStrings和AppSettings的区别 时间 2013-03-07 15:57:00 博客园精华区 原文 http://www.cnblogs.com/bindot ...
随机推荐
- java 23种设计模式 链接集锦
创建型抽象工厂模式 http://www.cnblogs.com/java-my-life/archive/2012/03/28/2418836.html工厂方法 http://www.cnblogs ...
- js判断是安卓 还是 ios webview
判断原理:JavaScript是前端开发的主要语言,我们可以通过编写JavaScript程序来判断浏览器的类型及版本.JavaScript判断浏览器类型一般有两种办法,一种是根据各种浏览器独有的属性来 ...
- 16Log4J
Log4J Log4j是Apache的一个开放源代码项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台.文件.GUI组件.甚至是套接口服务器.NT的事件记录器.UNIX Syslog守 ...
- 05Servlet example
dgdfgdfggggggg Servlet 表单数据 在客户端,GET通过URL提交数据,数据在URL中可见:POST把数据放在form的数据体内提交.GET提交的数据最多只有1024字节:POST ...
- iptables详解(1):iptables概念
所属分类:IPtables Linux基础 基础知识 常用命令 这篇文章会尽量以通俗易懂的方式描述iptables的相关概念,请耐心的读完它. 防火墙相关概念 此处先描述一些相关概念. 从逻辑上 ...
- C: 当字符数组首指针转化成char *指针,sizeof(*ptr)不为array的size
#include <stdio.h> #include <string.h> int main() { char a[10] = "\0"; char *p ...
- CentOS 6 Yum本地源配置
#cd /etc/yum.repos.d #rm CentOS-Base.repo CentOS-Base.repo 是yum 网络源的配置文件(默认) #vi CentOS-Media.repo C ...
- 洛谷 2824 [HEOI2016/TJOI2016]排序
[题意概述] 对一个1到n的排列做m次区间排序,最后询问位置q上面的数. [题解] 区间排序的效率是nlogn,所以暴力做的话效率是mnlogn,显然达不到要求. 我们考虑二分答案.如果某个位置的数比 ...
- 哈希表模板(Hash set)
省选前最后的复(chui si)习(zheng zha). 上模板吧 namespace Hash_Table{ #define inf ~0U>>1 #define MaxN 10010 ...
- Apache Maven Cookbook(八)学习笔记-Handling Typical Build Requirements
Including and excluding additional resources Using the Maven Help Plugin: mvn help:effective-pom mvn ...