What am I doing wrong with retrieving the appsettings?
in my app.config
i have:
...
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2"/>
</startup>
<appSettings>
<add key="culture" value="cs-CZ"/>
</appSettings>
</configuration>
i am attempting to retrieve the culture
value:
Dim culture = ConfigurationManager.AppSettings("culture").ToString()
But culture
is always yielding Nothing
What am I doing wrong with retrieving the appsettings?
Here's my entire app.config file:
<?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>
Your code is not wrong. And since ConfigurationManager.AppSettings("some-key")
always returns a string for an existing property, you never need the .ToString()
call.
The fact that it yields Nothing
means it's not there. Since the keys do seem to match, most likely your app.config is not in the correct location.
Are you sure it's in the project that generates your .exe?
Honestly there's no visible problem. I'd have to see your project's .vbproj xml to know for sure. I even double checked by creating a new empty VB project and copy-pasted your app.config over mine.
This is the whole code of the project:
Module Module1
Sub Main()
Dim culture = Configuration.ConfigurationManager.AppSettings("culture")
Console.WriteLine(culture)
Console.ReadKey()
End Sub
End Module
It nagged about the globalization
section being unknown so I removed that section and left the rest as-is.
Console app output as expected: cs-CZ
You could try removing that section and see what happens but I doubt that's the culprit. Your app wouldn't run otherwise.
So on to the .vbproj XML:
There's a specific way in which Visual Studio adds configurations. It typically doesn't work out-of-the-box when you manually add these kind of files through simple copy-paste.
Is this the app.config that was freshly added to the project when you created it, or is this a manual add / copy-paste?
You could verify this:
Right-click your project. press Unload Project
Right-click your project again, press Edit [projectname].vbproj
. You're now looking at the XML file that defines the project structure/settings etc.
Find the piece <None Include="App.config" />
and confirm that it sits under an ItemGroup
node together with two others, it should look roughly like this:
<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>
Again, I wouldn't expect this to be the issue because you said the application.exe.config
is correctly generated to the output. It doesn't hurt to eliminate the possibilty though.
.NET's configuration system is multi-inheritance starting at MACHINE.config
and going all the way down to your app.config
(with one or two layers of web/user context in between depending on the situation).
I don't know details beyond what you showed me, so technically it's possible that some settings on your computer / user profile / visual studio etc, make it so that the configuration hierarchy is loaded differently. Perhaps you're just getting a higher level config served.
You can "force" this with some slightly convoluted code. I wouldn't call it a solution, but at least it narrows down the issue if this actually works:
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