vb.net vs 2010 阅读配置设置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7167414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
vs 2010 reading configuration settings
提问by DeveloperM
Coding in VB.net in VS 2010. I have:
在 VS 2010 中的 VB.net 中编码。我有:
Imports System.Configuration
and I added a reference to System.Configuration.
Imports System.Configuration
我添加了对 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.
执行时失败,并显示“未将对象引用设置为对象的实例”。sDBName 已设置。
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:
我的 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>
采纳答案by Jim Wooley
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:
在 VB 中引用设置的方式与在 C# 中的引用方式略有不同。最简单的方法是使用作为项目一部分的设置,然后通过 My 命名空间引用它:
MessageBox.Show(My.Settings.sDBName)
(Note, you don't need the .ToString here because sDBName is already a string).
(注意,这里不需要 .ToString 因为 sDBName 已经是一个字符串)。
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:
由于您包含一个单独的 Settings 文件,您应该能够通过调用 Default 方法来获取实例,然后您的属性脱离默认值来访问其值:
MessageBox.Show(Settings1.Default.sDBName)
回答by Jon Egerton
You've said that sDBName
is set: Is the sDBName
setting included in the config of your main application, or just in the config of the assembly that the code is contained in?
您说过sDBName
已设置:sDBName
设置是包含在主应用程序的配置中,还是仅包含在包含代码的程序集的配置中?
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 中,该设置应出现在该appSettings
部分中,例如:
<appSettings>
<add key="sDBName" value="devDB"/>
</appSettings>
回答by julio
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
嗨,我有同样的错误,您检查解决方案只是图层并以图层形式保留 app.config 文件并从其他图层调用
1-reference to System.Configuration
2-Imports System.Configuration
so after you call
所以在你打电话之后
As String Dim TextoConexion
TextoConexion = ConfigurationManager.AppSettings.Item ("CONNECT"). ToString
The error occurs because you do not see "CONNECT" for example
发生错误是因为您没有看到“CONNECT”,例如
so is the app.config
app.config 也是如此
<appSettings>
<add key="CONEC" value="Server"/>
<add key="SERVIDOR" value="SQL2008R2"/>
</appSettings>
</configuration>
回答by Engr. S.M. Inuwa
You don't need to need to follow long procedures. The easiest way to do that is
您无需遵循冗长的程序。最简单的方法是
`msgBox(My.Settings.sDBName)`
this would give you value of sDBName
这会给你 sDBName 的值