vs 2010 阅读配置设置

发布于 2024-12-01 02:14:37 字数 828 浏览 1 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

醉殇 2024-12-08 02:14:37

在 VB 中引用设置与在 C# 中引用设置略有不同。最简单的方法是使用项目中的设置,然后通过 My 命名空间引用它:(

MessageBox.Show(My.Settings.sDBName)

注意,这里不需要 .ToString,因为 sDBName 已经是一个字符串)。

由于您包含一个单独的设置文件,因此您应该能够通过调用 Default 方法来访问其值,以获取实例,然后将您的属性从默认值中删除:

MessageBox.Show(Settings1.Default.sDBName)

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:

MessageBox.Show(My.Settings.sDBName)

(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:

MessageBox.Show(Settings1.Default.sDBName)
与往事干杯 2024-12-08 02:14:37

您说过 sDBName 已设置:sDBName 设置是否包含在主应用程序的配置中,或者仅包含在包含代码的程序集的配置中?

它需要位于入口点程序集的配置中,因为这是应用程序启动时加载的配置。

如果在那里设置,那么您应该发布您的配置,以便我们可以看到设置以检查问题。


根据评论进行更新:

在 app.config 中,该设置应出现在 appSettings 部分中,例如:

  <appSettings>
    <add key="sDBName" value="devDB"/>
  </appSettings>

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?

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:

  <appSettings>
    <add key="sDBName" value="devDB"/>
  </appSettings>
故乡的云 2024-12-08 02:14:37

您好,我有同样的错误,您检查解决方案只是层,并以层形式保留 app.config 文件,然后从其他层调用,

1-reference to System.Configuration
2-Imports System.Configuration

因此在调用后

As String Dim TextoConexion

TextoConexion = ConfigurationManager.AppSettings.Item ("CONNECT"). ToString

会发生错误,因为您没有看到“CONNECT”,例如

应用程序也是如此.config

<appSettings>
<add key="CONEC" value="Server"/>
<add key="SERVIDOR" value="SQL2008R2"/>
</appSettings>
</configuration>

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

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

so is the app.config

<appSettings>
<add key="CONEC" value="Server"/>
<add key="SERVIDOR" value="SQL2008R2"/>
</appSettings>
</configuration>
随心而道 2024-12-08 02:14:37

您无需遵循冗长的程序。最简单的方法是

`msgBox(My.Settings.sDBName)`

这将为您提供 sDBName 的值

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文