我如何从app.config文件中读取 [英] How do I read from the app.config file

查看:346
本文介绍了我如何从app.config文件中读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个app.config文件,我有一个网址,其列为



I have a app.config file that I have a url in and its listed as

<appSettings>
     <add key="web" value="http://URL HERE"/>
  </appSettings>





然后在我有的文件中



ChatMessengerService.Messenger messenger = new ChatMessengerService.Messenger();





messenger.Url = System.Configuration.ConfigurationManager.AppSettings [web];

bool loginSucces = messenger.Login(用户名,sha1HashStr);





然而它不读,但如果我硬编码它会工作得很好



任何帮助都会很好。



then in a file I have

ChatMessengerService.Messenger messenger = new ChatMessengerService.Messenger();


messenger.Url = System.Configuration.ConfigurationManager.AppSettings["web"];
bool loginSucces = messenger.Login(username, sha1HashStr);


however its not reading but if I hardcoded the url it will work just fine

Any help would be nice.

推荐答案

试试在这样的配置部分编写你的appsettings

Trye to write your appsettings in configuration secton like this
<configuration>
  <appSettings>
    <add key="web" value="http://URL HERE" />
  </appSettings>
</configuration>



然后使用System.Configuration添加以下引用

;

最后在你的代码中你必须这样写,

messenger.Url = ConfigurationManager.AppSettings [web];


Then add the following reference
using System.Configuration;
And finally in your code section you have to write like this,
messenger.Url = ConfigurationManager.AppSettings["web"];


如果你使用的是MVC,那么有2个网页.config文件可用。一个用于视图,另一个用于整个解决方案。请检查您添加了应用密钥的web.config。它应该是解决方案的web.config来获取值。
If you are using MVC, there are 2 web.config file available in it. One for Views and another for the whole solution. Kindly check in which web.config, you have added the app key. It should be solution's web.config for getting the value.


这篇关于我如何从app.config文件中读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆