[英]Reading a string from app.config file
My question isn't related to the connection string which is the most popular. 我的问题与最受欢迎的连接字符串无关。 The other only help I could find with this is someone not using the reference.
我可以找到的另一个唯一帮助是没有使用参考。
What I'm trying to do: I have a string stored in the app.config file, which looks like: 我想做的是:我在app.config文件中存储了一个字符串,看起来像:
<applicationSettings>
<App_5.Properties.Settings>
<setting name="Location_Name" serializeAs="String">
<value>String I want</value>
</setting>
My code to pull the string is: 我提取字符串的代码是:
string Loc_Name = ConfigurationManager.AppSettings["Location_Name"];
I have the reference added, and used. 我添加了参考,并使用了。 When I run the program my return is always
null
. 当我运行程序时,返回值始终为
null
。
You are storing it in the wrong place in the app.config. 您将其存储在app.config中的错误位置。 For single name/value pairs use the
appSettings
. 对于单个名称/值对,请使用
appSettings
。 Then your retrieval code in the question will work. 然后,您在问题中的检索代码将起作用。
<configuration>
<appSettings>
<add key="Location_Name" value="String I want" />
</appSettings>
</configuration>
Code (repeated from question) 代码(问题重复)
var value = ConfigurationManager.AppSettings["Location_Name"];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.