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

https://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings(v=vs.110).aspx

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

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的更多相关文章

  1. 点点滴滴-ConfigurationManager.AppSettings

    在写程序的配置文件,里面添加了几个配置,下面是appSettings节点的设置 <appSettings> <add key="StyleFolder" valu ...

  2. ConfigurationManager.AppSettings方法

    一 配置文件概述: 应用程序配置文件是标准的 XML 文件,XML 标记和属性是区分大小写的.它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而不必重编译应用程序.配置文件的根节点是conf ...

  3. ConfigurationManager.AppSettings 属性 appSettings

    https://msdn.microsoft.com/zh-cn/library/system.configuration.configurationmanager.appsettings(v=vs. ...

  4. Asp.Net Core 中无法使用 ConfigurationManager.AppSettings

    刚刚接触.net core ,准备把之前的一些技术常用工具先移植到.net Standard上面来, 方便以后使用,结果用到ConfigurationManager 的 AppSettings 就出现 ...

  5. ConfigurationManager 读写AppSettings键值对

    using System; using System.Configuration; namespace ConsoleApplication1 { class Program { static voi ...

  6. System.ConfigurationManager类用于对配置文件的读取

    http://blog.csdn.net/ligenyingsr/article/details/54095986 System.ConfigurationManager类用于对配置文件的读取.其具有 ...

  7. 在Asp.Net MVC 中如何用JS访问Web.Config中appSettings的值

    应用场景: 很多时候我们要在Web.Config中添加appSettings的键值对来标识一些全局的信息,比如:调用service的domain,跳转其他网站页面的url 等等: 那么此时就涉及到了一 ...

  8. AppSettings和connectionStrings的却别(转)

    AppSettings是ASP.NET1.1时期用的,在.NET Framework 2.0中,新增了ConnectionStrings. 1.<connectionStrings> &l ...

  9. <connectionStrings> <appSettings> 读取方法

    C#中ConnectionStrings和AppSettings的区别 时间 2013-03-07 15:57:00  博客园精华区 原文  http://www.cnblogs.com/bindot ...

随机推荐

  1. java环境初级部署及项目搭建

    一.网页地址 Java各版本下载地址: http://www.oracle.com/technetwork/cn/java/archive-139210-zhs.html Eclipse官方下载地址: ...

  2. for 循环新的写法==列表解析

    1. (for x in L1) 是一个可迭代对象: 2. 列表解析比for 循环快,列表解析的迭代在解释器内部是以C语言速度执行, 而不是手动python代码执行: (x+10 for x in L ...

  3. Android写入到mysql里的中文总是乱码?

    中文编码的问题总是让人头疼,之前在python爬虫就折腾得死去活来,现在写app又是这样. 总结下来,就是三点吧: 数据库: 确定字符编码是utf8, collate: utf8_general_ci ...

  4. Spring核心技术(三)——Spring的依赖及其注入(续)

    本文将继续前文,针对依赖注入的细节进行描述 依赖注入细节 如前文所述,开发者可以通过定义Bean的依赖的来引用其他的Bean或者是一些值的,Spring基于XML的配置元数据通过支持一些子元素< ...

  5. CodeForcesGym 100753A A Journey to Greece

    A Journey to Greece Time Limit: 5000ms Memory Limit: 262144KB This problem will be judged on CodeFor ...

  6. h5dnd sortable mutil groups

    h5dnd sortable mutil groups https://codepen.io/webgeeker/pen/JmPXaN https://codepen.io/webgeeker/pen ...

  7. 转载 - JSONObject简介

    出处: http://www.cnblogs.com/java-pan/archive/2012/04/07/JSONObject.html JSONObject简介   本节摘要:之前对JSON做了 ...

  8. 【BZOJ2434】阿狸的打字机(fail树,DFS序)

    题意: 1<=N<=10^5 1<=M<=10^5 输入总长<=10^5   思路: From http://blog.csdn.net/lych_cys/article ...

  9. java数组知识总结(一)//按类

    在线api  目录: 零/数组(基本元素) 1.  声明一个数组 2.  创建一个数组 3.  数组名.length 4.  数组的引用 一/java.lang.reflect.Array     / ...

  10. code wars quiz: toInteger

    Your task is to program a function which converts any input to an integer. Do not perform rounding, ...