0

I am changing the configuration file in Visual Studio,the program language is C#,and the enviroment is .net framework 4.0+windows 7.this is my code:

        #region change configuration file
        /// <summary>
        /// 
        /// </summary>
        /// <param name="createdTime"></param>
        [TestCase("2015-06-12 14:37:59")]
        public void ChangeConfiguration(string createdTime)
        {
            string str = System.Environment.CurrentDirectory+@"\App.config";   
            string appDomainConfigFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
            Configuration config = ConfigurationManager.OpenExeConfiguration(str);          
            AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");            
            appSettings.Settings.Remove("queryTime");
            appSettings.Settings.Add("queryTime", createdTime); 
            config.Save();
            ConfigurationManager.RefreshSection("configuration");
        }
        #endregion

And the program generate a new configuration file named:App.config.config.How could this being?I just want to modify the queryTime in original file.

ps:This is my App.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="interval" value="5"/>
    <add key="queryTime" value="2015-06-10 14:37:59"/>
    <add key="_TimerInterval" value="5000"/>
  </appSettings>
</configuration>
Dolphin
  • 29,069
  • 61
  • 260
  • 539
  • Possible duplicate of [this](http://stackoverflow.com/questions/3838027/how-to-update-an-value-in-app-config-file) and [this](http://stackoverflow.com/questions/11149556/c-sharp-app-config-change-value) – Amnesh Goel Jun 15 '15 at 03:43

1 Answers1

2

This is because OpenExeConfiguration takes the Exe name as input and automatically looks for a config file name Exe.config (This is the default format). -

   // Get the configuration file. The file name has 
  // this format appname.exe.config.
  System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(exePath);

You can check the sample code here - https://msdn.microsoft.com/en-us/library/ms224437(v=vs.110).aspx

So in your case just remove .config at the end of the file name -

    #region change configuration file
    /// <summary>
    /// 
    /// </summary>
    /// <param name="createdTime"></param>
    [TestCase("2015-06-12 14:37:59")]
    public void ChangeConfiguration(string createdTime)
    {
        string str = System.Environment.CurrentDirectory+@"\App";  //remove .config  
        string appDomainConfigFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
        Configuration config = ConfigurationManager.OpenExeConfiguration(str);          
        AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");            
        appSettings.Settings.Remove("queryTime");
        appSettings.Settings.Add("queryTime", createdTime); 
        config.Save();
        ConfigurationManager.RefreshSection("configuration");
    }
    #endregion
brainless coder
  • 6,310
  • 1
  • 20
  • 36