【问题标题】:unable to retrieve appsettings value无法检索 appsettings 值
【发布时间】:2018-03-05 08:26:11
【问题描述】:

我在检索应用设置时做错了什么?

在我的app.config 我有:

...
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2"/>
</startup>
  <appSettings>
    <add key="culture" value="cs-CZ"/>
  </appSettings>
</configuration>

我正在尝试检索 culture 值:

Dim culture = ConfigurationManager.AppSettings("culture").ToString()

但是culture 总是让步Nothing

我在检索应用设置时做错了什么?

这是我的整个 app.config 文件:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <globalization uiculture="cs-CZ" culture="cs-CZ">
  </globalization>
  <configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
            <section name="WindowsApplication1.My.MySettings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false"/>
        </sectionGroup>
    </configSections>

    <system.diagnostics>
        <sources>
            <!-- This section defines the logging configuration for My.Application.Log -->
            <source name="DefaultSource" switchName="DefaultSwitch">
                <listeners>
                    <add name="FileLog"/>
                    <!-- Uncomment the below section to write to the Application Event Log -->
                    <!--<add name="EventLog"/>-->
                </listeners>
            </source>
        </sources>
        <switches>
            <add name="DefaultSwitch" value="Information"/>
        </switches>
        <sharedListeners>
            <add name="FileLog" type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" initializeData="FileLogWriter"/>
            <!-- Uncomment the below section and replace APPLICATION_NAME with the name of your application to write to the Application Event Log -->
            <!--<add name="EventLog" type="System.Diagnostics.EventLogTraceListener" initializeData="APPLICATION_NAME"/> -->
        </sharedListeners>
    </system.diagnostics>
    <userSettings>
        <WindowsApplication1.My.MySettings>
            <setting name="txtPatientName" serializeAs="String">
                <value>Patient Name</value>
            </setting>
            <setting name="datastorage" serializeAs="String">
                <value/>
            </setting>
        </WindowsApplication1.My.MySettings>
    </userSettings>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2"/>
</startup>
  <appSettings>
    <add key="culture" value="cs-CZ"/>
  </appSettings>
</configuration>

【问题讨论】:

    标签: .net vb.net visual-studio-2017 configurationmanager appsettings


    【解决方案1】:

    您的代码没有错。由于ConfigurationManager.AppSettings("some-key") 总是为现有属性返回一个字符串,因此您永远不需要调用.ToString()

    它产生Nothing 的事实意味着它不存在。由于键似乎确实匹配,因此您的 app.config 很可能不在正确的位置。

    您确定它在生成您的 .exe 的项目中吗?

    编辑:

    老实说,没有明显的问题。我必须查看您项目的 .vbproj xml 才能确定。我什至通过创建一个新的空 VB 项目并在我的上复制粘贴你的 app.config 来仔细检查。

    这是项目的全部代码:

    Module Module1
        Sub Main()
            Dim culture = Configuration.ConfigurationManager.AppSettings("culture")
            Console.WriteLine(culture)
            Console.ReadKey()
        End Sub
    End Module
    

    它抱怨 globalization 部分未知,所以我删除了该部分,其余部分保持原样。

    按预期控制台应用程序输出:cs-CZ

    您可以尝试删除该部分,看看会发生什么,但我怀疑这是罪魁祸首。否则您的应用将无法运行。

    继续 .vbproj XML:

    Visual Studio 添加配置有一种特定的方式。当您通过简单的复制粘贴手动添加此类文件时,它通常无法开箱即用。

    这是您创建项目时新添加到项目中的 app.config,还是手动添加/复制粘贴?

    您可以验证这一点:

    1. 右键单击您的项目。按Unload Project

    2. 再次右键单击您的项目,按Edit [projectname].vbproj。您现在正在查看定义项目结构/设置等的 XML 文件。

    3. 找到&lt;None Include="App.config" /&gt; 并确认它与另外两个位于ItemGroup 节点下,大致如下所示:

       <ItemGroup>
         <None Include="My Project\Application.myapp">
           <Generator>MyApplicationCodeGenerator</Generator>
           <LastGenOutput>Application.Designer.vb</LastGenOutput>
         </None>
         <None Include="My Project\Settings.settings">
           <Generator>SettingsSingleFileGenerator</Generator>
           <CustomToolNamespace>My</CustomToolNamespace>
           <LastGenOutput>Settings.Designer.vb</LastGenOutput>
         </None>
         <None Include="App.config" />
       </ItemGroup>
      

    同样,我不认为这是问题,因为您说application.exe.config 已正确生成到输出。不过,消除这种可能性并没有什么坏处。

    最后,范围界定问题:

    .NET 的配置系统是多继承的,从 MACHINE.config 一直到您的 app.config(根据具体情况,中间有一层或两层网络/用户上下文)。

    除了您向我展示的内容外,我不知道详细信息,因此从技术上讲,您的计算机/用户配置文件/Visual Studio 等上的某些设置可能会导致配置层次结构的加载方式有所不同。也许您只是获得了更高级别的配置服务。

    你可以用一些稍微复杂的代码来“强制”这个。我不会称其为解决方案,但如果这确实有效,至少它可以缩小问题范围:

    Imports System.Configuration
    Module Module1
        Sub Main()
            Dim config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
            Dim culture = config.AppSettings.Settings("culture").Value 'Note you do need the .Value here; this is a different kind of config object
            Console.WriteLine(culture)
            Console.ReadKey()
        End Sub
    End Module
    

    【讨论】:

    • 谢谢。我怎么知道它应该在哪个位置
    • @l--''''''------'''''''''''' 通常是启动项目的根目录。假设它是一个桌面应用程序,在构建/调试时,您应该在项目的 bin\debug 文件夹(与 .exe 结束的位置相同)中得到一个名为 [project name].exe.config 的文件。这应该包含您的 app.config 中的内容 - 这是确定是您的代码错误还是项目设置错误的好方法。
    • 非常感谢您的帮助,它确实正确命名了配置文件并正确输出了它。我已将整个配置文件包含在更新的问题中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-03
    • 2019-01-23
    • 1970-01-01
    • 1970-01-01
    • 2012-03-25
    • 1970-01-01
    相关资源
    最近更新 更多