How to read app.config file in VB.net?
In this tutorial, I will show you how to read the app.config file in VB.Net.
There are 2 ways to store data in config file, you can save it in
- appSettings
- userSettings
You can use this interface to configure setting in userSetting
1. Sample Config file with 2 different method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | <?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" > <section name="ReadConfig.My.MySettings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" /> </sectionGroup> </configSections> <appSettings> <add key="URL" value="http://instinctcoder.com" /> </appSettings> <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> <ReadConfig.My.MySettings> <setting name="SettingURL" serializeAs="String"> <value>http://instinctcoder.com</value> </setting> </ReadConfig.My.MySettings> </userSettings> </configuration> |
2. Read app.config File In VB.Net
There are few ways to read app.config file in vb.net
Method 1
1 | System.Configuration.ConfigurationManager.AppSettings.Get("URL") |
Method 2
1 | My.Settings.SettingURL |
Method 3
1 2 | Dim reader As New System.Configuration.AppSettingsReader MsgBox(reader.GetValue("URL", GetType(String))) |
Method 4 – The obsolete way
When you type below in your code it will appear message to use method 1.
1 | System.Configuration.ConfigurationSettings.AppSettings.Get("URL") |
Tips:
I personally preferred method 3, because when there is more than one project in a solution this method allows you to read from any project in your solution.