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 ...
随机推荐
- R中矩阵运算
# 数据产生 # rnorm(n, mean = 0, sd = 1) 正态分布的随机数(r 代表随机,可以替换成dnorm, pnorm, qnorm 作不同计算.r= random = 随机, d ...
- Java基础(六)--枚举Enum
枚举: 刚开始项目中没怎么用过,只知道能够实现作为项目中类似定义Constant的功能,然后知道枚举实现的单例模式几乎是最优雅的,所以, 想要深入完整的了解一下 1.基本特性: Enum.values ...
- 浅谈IFC
IFC布局规则: 在一个行内格式化上下文中,盒是一个接一个水平放置的,从包含块的顶部开始 这些盒之间的水平margin,border和padding都有效 盒可能以不同的方式竖直对齐:以它们的底部或者 ...
- 小程序 textarea ios兼容解决
今天遇到,在小程序里textarea会存在一定的兼容性问题,textarea有默认的内边距,在安卓和ios显示的时候,ios边距会比安卓的大很多. 解决办法: 通过 wx.getSystemInfoS ...
- 小程序接口越过域名和https限制方法
都知道小程序上线接口需要域名,还需要https,就算是体验版的都是需要的,这样就筛选掉一大批开发者,像我这样只有学生轻量级服务器的学生要开发自己的小程序就很为难,但今天确惊奇的在小程序社区里面找到了用 ...
- pygame 方块随机飞舞动画
import pygame import random # default WIDTH=1280 HEIGHT=1060 FPS=60 sum=0 # set color WHITE=(255,255 ...
- X shell 6下载安装和简单使用
①前言:昨天已经上线了的智能家居项目出现了一个BUG,需要重新写个html发布到服务器上,由于公司大佬都在忙别的项目,时间比较紧张,这种小事就落到了我这个小喽啰身上.其实,写个html我还是可以接受的 ...
- ebay 如何获取用户token
1. 首先 配置环境加载依赖的ebay SDK 下载地址 https://go.developer.ebay.com/ebay-sdks 需要在本地仓库安装下面的jar mvn install:ins ...
- Spark在Executor上的内存分配
spark.serializer (default org.apache.spark.serializer.JavaSerializer ) 建议设置为 org.apache.spark.ser ...
- 神器的方块Magic Squares
题目背景 在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 5 题目描述 我们知道魔板的每一个方格都有一种颜色.这8种颜 ...