检索appsettings我做错了什么?
在我的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
检索appsettings我做错了什么?
这是我的整个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>
你的代码没错。由于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,还是手动添加/复制粘贴?
你可以验证这个:
Unload Project
Edit [projectname].vbproj
。您现在正在查看定义项目结构/设置等的XML文件。<None Include="App.config" />
并确认它与其他两个一起位于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
(根据情况,中间有一层或两层web /用户上下文)。
我不知道你向我展示的细节,所以从技术上来说,你的计算机/用户配置文件/可视工作室等的某些设置可能使得配置层次结构的加载方式不同。也许你只是获得更高级别的配置服务。
你可以用一些稍微复杂的代码“强制”这个。我不会称之为解决方案,但至少它会缩小问题,如果这实际上有效:
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