vs 2010 阅读配置设置
在 VS 2010 中用 VB.net 进行编码。我有:
导入 System.Configuration
并且添加了对 System.Configuration 的引用。
时
**MsgBox(ConfigurationManager.AppSettings("sDBName").ToString)**
执行 失败,并显示“对象引用未设置到对象的实例”。 sDBName 已设置。
我错过了什么?
回复:
很抱歉延迟回复您;其他事情需要我注意。
我的 app.config 文件中没有这样的部分。我通过Settings1.settings文件添加了sDBName和其他设置;这些对象自动显示在 app.cong 中,如下所示:
<applicationSettings>
<QuickRequest.Settings1>
<setting name="sDBName" serializeAs="String">
<value>xxx</value>
</setting>
<setting name="sInputPath" serializeAs="String">
<value>c:\yyy\Infile\</value>
</setting>
</QuickRequest.Settings1>
Coding in VB.net in VS 2010. I have:
Imports System.Configuration
and I added a reference to System.Configuration.
When
**MsgBox(ConfigurationManager.AppSettings("sDBName").ToString)**
is executed, it fails, with "Object reference not set to an instance of an object."
sDBName is set.
What have I missed?
In response:
Sorry for the delay in getting back to you; other things demanded my attention.
There is no such section in my app.config file. I added sDBName and other settings via the Settings1.settings file; these objects automatically show up in app.cong as follows:
<applicationSettings>
<QuickRequest.Settings1>
<setting name="sDBName" serializeAs="String">
<value>xxx</value>
</setting>
<setting name="sInputPath" serializeAs="String">
<value>c:\yyy\Infile\</value>
</setting>
</QuickRequest.Settings1>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 VB 中引用设置与在 C# 中引用设置略有不同。最简单的方法是使用项目中的设置,然后通过 My 命名空间引用它:(
注意,这里不需要 .ToString,因为 sDBName 已经是一个字符串)。
由于您包含一个单独的设置文件,因此您应该能够通过调用 Default 方法来访问其值,以获取实例,然后将您的属性从默认值中删除:
You reference settings in VB slightly differently than you do in C#. The easiest way is to use the settings that are part of the project and then reference it through the My namespace:
(Note, you don't need the .ToString here because sDBName is already a string).
Since you are including a separate Settings file, you should be able to access its values by calling the Default method to get the instance and then your property off of the default:
您说过
sDBName
已设置:sDBName
设置是否包含在主应用程序的配置中,或者仅包含在包含代码的程序集的配置中?它需要位于入口点程序集的配置中,因为这是应用程序启动时加载的配置。
如果在那里设置,那么您应该发布您的配置,以便我们可以看到设置以检查问题。
根据评论进行更新:
在 app.config 中,该设置应出现在
appSettings
部分中,例如:You've said that
sDBName
is set: Is thesDBName
setting included in the config of your main application, or just in the config of the assembly that the code is contained in?It needs to be in the config of the entrypoint assembly as this is the config loaded when your application starts.
If it is set there, then you should post your config so that we can see the setting to check for problems.
Update in response to comment:
In the app.config, the setting should appear in the
appSettings
section, for example:您好,我有同样的错误,您检查解决方案只是层,并以层形式保留 app.config 文件,然后从其他层调用,
因此在调用后
会发生错误,因为您没有看到“CONNECT”,例如
应用程序也是如此.config
Hi I have the same error you check the solution is just layers and leave a app.config file in layer form and call from the other layers
so after you call
The error occurs because you do not see "CONNECT" for example
so is the app.config
您无需遵循冗长的程序。最简单的方法是
这将为您提供 sDBName 的值
You don't need to need to follow long procedures. The easiest way to do that is
this would give you value of sDBName