无法从appconfig获取价值 [英] Can't get value from appconfig

查看:108
本文介绍了无法从appconfig获取价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好.我正在尝试从我的appconfig文件中获取价值,但我做不到.到目前为止,这是我做过的事情:

-右键单击添加了appconfig到项目-添加-新项目-应用程序配置文件
-创建一个appSettings节点并向其添加键和值:

Hello. I''m trying to get value from my appconfig file but i can''t. Here is what i''ve done so far:

-Added appconfig by right click to project-add-new item-Application Configuration File
-Created an appSettings node and added a key and value to it:

<configuration>
  <appSettings>
    <add key="openwin" value="false"/>
  </appSettings>
</configuration>


-添加了System.Configuration作为参考
-我在程序中的密钥的调用值:


-Added System.Configuration as Referance
-Called value of my key in program:

 Dim appValue As String
appValue = System.Configuration.ConfigurationManager.AppSettings("openwin")
MsgBox(appValue)



这应该给我一个假"值,但不是,它只是给我一个无".



This should give me the "false" value but it doesn''t, it simply giving "Nothing" to me.

推荐答案

如果您的解决方案中有多个项目,并说启动项目是webapplication,并且您具有以下代码

If you having multiple project in your solution, and say the startup project is webapplication and you having the below code

<add key="openwin" value="false" />



数据访问层(DAL)中的[类库项目]意味着您不必在启动项目中添加它,例如在此将其作为Web应用程序的web.config文件添加.



in Data Access Layer (DAL)[class library project] means that doesn''t make any sense you have to add that in startup project say here as webapplication''s web.config file.


我找到了问题,问题是我的配置文件与我的exe不在同一个文件夹中,因此应命名为ApplicationName.ApplicationType.config,如myprogram.exe.config

感谢您提供Wes Aday链接!
I found the problem, the problem was my config file wasn''t in the same folder with my exe and it should be named as ApplicationName.ApplicationType.config like myprogram.exe.config

Thanks for the link Wes Aday!


尝试以下操作:

Try with the following:

System.Configuration.ConfigurationSettings.AppSettings["openwin"].ToString();
OR
(string)System.Configuration.ConfigurationSettings.AppSettings.Get("openwin");


这篇关于无法从appconfig获取价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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