Update App.config file using vb.net

3.4k Views Asked by At

I have an App.config with this code

<appSettings>
  <add key="dataPath" value="" />
  <add key="databaseConnectionString" value="MultipleActiveResultSets=True;SERVER=;DATABASE=;UID=;PWD=" />
  <add key="APIaddress" value="" />
  <add key="Timer" value="1" />
  <add key="BufferSize" value="100" />
  <add key="fisrtConfig" value="1" />
  <add key="ClientSettingsProvider.ServiceUri" value="" />
</appSettings>

I can access to these values with this code

Srv = System.Configuration.ConfigurationManager.AppSettings("APIaddress").ToString()
timer = CInt(System.Configuration.ConfigurationManager.AppSettings("TimerEsecuzione").ToString())
connectionString = System.Configuration.ConfigurationManager.AppSettings("databaseConnectionString").ToString()
BufferSize = System.Configuration.ConfigurationManager.AppSettings("BufferSize").ToString()
isFirstConfig = IIf(System.Configuration.ConfigurationManager.AppSettings("fisrtConfig").ToString() = 1, True, False)

During my code run, I would to update one of these. I tried differents type of code but no one can update it. These are code I tried:

  • First

    Dim configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
    Dim settings = configFile.AppSettings.Settings
    settings("fisrtConfig").Value = 0
    configFile.Save(ConfigurationSaveMode.Modified)
    ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name)
    
  • Second

    ConfigurationManager.AppSettings.Item("fisrtConfig") = 0
    

Can you help me please?

0

There are 0 best solutions below