使用AppConfig时出错

时间:2014-10-03 15:53:52

标签: .net vb.net app-config

我从AppSettings访问字符串变量时遇到问题。以下是生成众所周知错误的代码:未设置为对象实例的对象引用

Dim folderName As String = ConfigurationManager.AppSettings("LogFolder").ToString()

这是一个App.config文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        ...
    </configSections>
  <connectionStrings>
    ...
  </connectionStrings>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <applicationSettings>
        <BazyPolaczenia.My.MySettings>
            <setting name="LogFile" serializeAs="String">
                <value>"log.txt"</value>
            </setting>
            <setting name="LogFolder" serializeAs="String">
                <value>"d:\Logs"</value>
            </setting>
        </BazyPolaczenia.My.MySettings>
    </applicationSettings>
</configuration>

我添加了Imports System.Configuration。这条线有什么问题?

1 个答案:

答案 0 :(得分:2)

您的配置文件不正确:

<configuration>
  <configSections>
    ...
  </configSections>
  <appSettings>
    <add key="LogFile" value="log.txt" />
    <add key="LogFolder" value="d:\Logs" />
  </appSettings>
 <connectionStrings>
  ...
  </connectionStrings>
</configuration>

如果您正在运行一个网站,您需要在web.config或入口点项目的app.config中(例如,执行的项目)