[英]Reading from a app.config file
I am trying to print to Console.Write
the value of the key name
from the following app.config
file. 我正在尝试打印到
Console.Write
来自以下app.config
文件的键name
的值。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="name" value="Chan" />
</appSettings>
</configuration>
C# code : C#代码:
Console.Write(ConfigurationManager.AppSettings["name"]);
Nothing gets printed in the console. 控制台中没有打印任何内容。 Why is this ?
为什么是这样 ?
Note: I have added a reference to the System.Configuration
dll 注意:我添加了对
System.Configuration
dll的引用
below code gives you the content of active config file. 下面的代码为您提供活动配置文件的内容。
var content = File.ReadAllLines(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
Check what you get as content
, is it contain key="name" value="Chan"
or something else ? 检查你得到的
content
,是否包含key="name" value="Chan"
或其他什么?
if you given <add key="name" value="Chan" />
then ConfigurationManager.AppSettings["name"] should return as Chan
如果你给
<add key="name" value="Chan" />
那么ConfigurationManager.AppSettings [“name”]应该返回为Chan
Given that your XML file (app.config) is properly formatted , try below. 鉴于您的XML文件(app.config) 格式正确 ,请尝试以下操作。
Declare a variable and assign the variable the AppSettings value. 声明一个变量并为变量分配AppSettings值。 Similar like-
类似的 -
string sName = "";
sName = ConfigurationManager.AppSettings["name"].ToString();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.