1

我有的是app.config这个

<?xml version="1.0"?>
<configuration>
   <configSections>
      <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
          <section name="Porject.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
     </sectionGroup>
</configSections>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup>
  <applicationSettings>
    <Porject.Properties.Settings>
        <setting name="PhotoLocation" serializeAs="String">
            <value>.\photos\</value>
        </setting>
    </Porject.Properties.Settings>
</applicationSettings>

<connectionStrings>
   <add name="****" connectionString="Data Source=***;Initial Catalog=****;User ID=***;Password=***" />
</connectionStrings>

</configuration>

这就是我调用 PhotoLocation 的方式:

  string s = ConfigurationManager.AppSettings["PhotoLocation"];

当我请求获取连接字符串时,没有问题并且工作正常;但是当我要求PhotoLocation它返回null。同样ConfigurationManager.AppSettings.Count返回 0。

有人知道我做错了什么吗?

4

2 回答 2

5

只需将其添加到您的应用程序配置文件中

<appSettings>

    <add key="PhotoLocation" value=".\photos\"/>

<appSettings>
于 2012-05-10T11:25:15.753 回答
2

ConfigurationManager.AppSettings读取(顾名思义)AppSettings您的配置块。

鉴于您已经创建了自己的部分,您希望使用var section = ConfigurationManager.GetSection("Porject.Properties.Settings")和读取该部分中的值。

于 2012-05-10T11:30:05.850 回答