0

I have an app.config in my project and I want to add some items, but I don't want to use the "appSettings" section. I want to create dynamically in my vb.net code, the section "items", to verify if it exists and to know if it has some keys in this order:

Dim config As Configuration = 
    ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath)

Dim items As CustomConfigSection = 
    (CustomConfigSection)configFile.GetSection("Items")

Dim keyItems = ConfigurationManager.AppSettings("Items")
Dim i = 0

for each Section in config.GetSection()

    if Not config.GetSection("Items") then
        config.Section.Add("Items")

    end if

    if Not config.AppSettings(Items).Settings.count = 0 then
        config.AppSettings(Items).Settings.Add("Items", myVar)
    else
        for each key in keyItems
            i += 1
            config.AppSettings.Settings.Add("Items" + i, myVar)
            config.Save(ConfigurationSaveMode.Modified)
            ConfigurationManager.RefreshSection("Items")
        next
    end if
    else

to look like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <Items>
    <add key="Items" value="val1"></add>
    <add key="Items1" value="val2"></add>
    <add key="Items2" value="val3"></add>
  </Items>
<configuration>

But I don't know how to manipulate the app.config function and I really don't want to add manually my sections.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.