VB.NET Modify value in App.config example

PLCITS
PLCITS used Ask the Experts™
on
Using VB.NET  (Visual Studio 2019) in windows form how I can update the app.config file?  somebody know an example
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Éric MoreauSenior .Net Consultant
Top Expert 2016
Commented:
the old System.Configuration can do it for you:
Option Strict On

Imports System.Configuration

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ShowConfig()
    End Sub

    Private Sub ShowConfig()
        ListBox1.Items.Clear()
        For Each key As String In ConfigurationManager.AppSettings
            Dim value As String = ConfigurationManager.AppSettings(key)
            ListBox1.Items.Add($"Key: {key}, Value: {value}")
        Next
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim config As System.Configuration.Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
        config.AppSettings.Settings.Add("Modification Date", DateTime.Now.ToLongTimeString() & " ")
        config.Save(ConfigurationSaveMode.Modified)
        ConfigurationManager.RefreshSection("appSettings")
        ShowConfig()
    End Sub
End Class

Open in new window

Author

Commented:
Hi thank you, It works perfect but another question what about if the config is in another path? if I select the config I want to modify?

An example!! I have a little program to select the config (any config)  and then open show the info in the screen and then update the info, is possible?


thanks again
Senior .Net Consultant
Top Expert 2016
Commented:
You could use the ConfigurationManager.OpenExeConfiguration (String) to load from a different path: https://www.codeproject.com/Articles/19675/Cracking-the-Mysteries-of-NET-2-0-Configuration#t2_1

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial