0

I'm giving .config file path and i want to retrieve appSetting value for key=MYDATA from that given .config file.

I tried following code but not getting expected.

//System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

        //var classLibrary1AppSettings = (System.Collections.Specialized.NameValueCollection)System.Configuration.ConfigurationManager.GetSection("appSettings");

        //config.AppSettings.File = "C:\\mydemo\\web.config";

want to get value for key=MYDATA

Neo
  • 15,491
  • 59
  • 215
  • 405

1 Answers1

3

Finally I able to manage it , posting will help others

System.Configuration.ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
            configFileMap.ExeConfigFilename = "C:\\mydemo\\web.config";

            System.Configuration.Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
            AppSettingsSection section = (AppSettingsSection)configuration.GetSection("appSettings");
            if (section.Settings.AllKeys.Any(key => key == "MYDATA"))
            {
                section.Settings["MYDATA"].Value = updateConfigId;
                configuration.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection("appSettings");
            }
Neo
  • 15,491
  • 59
  • 215
  • 405