none
Reload application config file at runtime

    Question

  • Hello,

    Please explain me this strange behavior of application config file. I start the program with such a file (all in bin\Debug directory):


    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <configSections>
      </configSections>
      <connectionStrings>
        <add name="FMDatabase" connectionString="Data Source=C:\file.sdf;Password=XXX;Persist Security Info=True"
          providerName="Microsoft.SqlServerCe.Client.3.5" />
      </connectionStrings>
    </configuration>
    


    Then in my application I modify this file and I get:


    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <configSections>
      </configSections>
      <connectionStrings>
        <clear />
        <add name="LocalSqlServer" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
          providerName="System.Data.SqlClient" />
        <add name="FMDatabase" connectionString="Data Source=C:\file.sdf;Password=YYY;Persist Security Info=True"
          providerName="Microsoft.SqlServerCe.Client.3.5" />
      </connectionStrings>
    </configuration>
    


    (one string added + change in password in the original connectrionstring)
    But if I want to display connection string from config file with this code:


    ConnectionStringSettingsCollection settings = ConfigurationManager.ConnectionStrings;
    if (settings != null)
    {
        MessageBox.Show("Count: " + settings.Count.ToString());
        foreach (ConnectionStringSettings cs in settings)
            MessageBox.Show("Name: " + cs.Name + "\n\nProvider: " + cs.ProviderName + "\n\nString: " + cs.ConnectionString);
    }
    


    I still get old data (i.e. old password).

    QUESTIONS:
    1. What should I do to "reload" config file thus to be able to read modified data? Currently I can do that only by restarting whole application.
    2. Why every time I try to modify my original connectrionstring LocalSqlServer is added? Code I use for modifyng connectrionstring is as follows:


    private static void WriteConnectrionString(string name, string constring, string provider)
    {
        Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        ConnectionStringsSection section = config.ConnectionStrings;
        ConnectionStringSettings newCs = new ConnectionStringSettings(name, constring, provider);
        try
        {
            section.ConnectionStrings.Remove(newCs); //remove if the same string exists in the settings
        }
        catch
        {
            //do nothing
        }
        finally
        {
            section.ConnectionStrings.Add(newCs);
        }
        section.SectionInformation.ForceSave = true;
        config.Save(ConfigurationSaveMode.Full);
    }
    


    Thanks in advance.











    Tuesday, April 07, 2009 8:58 PM

Answers

  • Hi hikari  ,

    //here you  must  add something like this 
    
     ConfigurationManager.RefreshSection("connectionStrings") 
    
    
    //Refreshes the named section so the next time that it is retrieved it will be re-read from disk. 
    ConnectionStringSettingsCollection settings = ConfigurationManager.ConnectionStrings;
    
    if (settings != null)
    
    {
    
        MessageBox.Show("Count: " + settings.Count.ToString());
    
        foreach (ConnectionStringSettings cs in settings)
    
            MessageBox.Show("Name: " + cs.Name + "\n\nProvider: " + cs.ProviderName + "\n\nString: " + cs.ConnectionString);
    
    }
    
    
    
    

     

    Hope this Help 


    A man's dreams are an index to his greatness
    • Proposed as answer by BRAHIM kamel Wednesday, April 08, 2009 8:15 AM
    • Marked as answer by Hikari PL Wednesday, April 08, 2009 10:59 AM
    Wednesday, April 08, 2009 8:10 AM
  • Hikari,

    The reason you're getting LocalSqlServer is because it's probably coming from your machine.config. The machine.config, app/web.config are merged when you open the config file via the Configuration API. If you don't want this, add a <clear/> element to your <connectionStrings> section in the app/web.config file.

    Regarding your other issue, I was getting an error message complaining about the configProtectionProvider attribute that is added to the <connectionStrings> element when you encrypt. Not sure if this is the error you're getting, but if so, here's how I got around it.

    Dave
    • Marked as answer by Hikari PL Friday, April 17, 2009 9:54 AM
    Thursday, April 16, 2009 2:28 PM

All replies

  • Is this config file is in the main web project or it is in some referenced project. If it is, you must place the connection string in the main project, otherwise any change in referenced project config file is unusefull.
    MCDBA, MCSD, MCITP DD&DA http://sharpsource.blogspot.com/
    Tuesday, April 07, 2009 9:48 PM
  • This is regular Windows Forms (not web!) project. The project config is kept in app.config file. For the purpose of testing I always run the program form bin/debug directory, in my case:
    Finance Manager.exe
    Finance Manager.pdb
    Finance Manager.exe.Config

    The XML extracts I've provided you with are from Finance Manager.exe.Config.

    P.S.
    By restarting the application I mean quiting Finance Manager.exe and running it once again (not through Visual Studio IDE!).
    Wednesday, April 08, 2009 6:40 AM
  • Hi hikari  ,

    //here you  must  add something like this 
    
     ConfigurationManager.RefreshSection("connectionStrings") 
    
    
    //Refreshes the named section so the next time that it is retrieved it will be re-read from disk. 
    ConnectionStringSettingsCollection settings = ConfigurationManager.ConnectionStrings;
    
    if (settings != null)
    
    {
    
        MessageBox.Show("Count: " + settings.Count.ToString());
    
        foreach (ConnectionStringSettings cs in settings)
    
            MessageBox.Show("Name: " + cs.Name + "\n\nProvider: " + cs.ProviderName + "\n\nString: " + cs.ConnectionString);
    
    }
    
    
    
    

     

    Hope this Help 


    A man's dreams are an index to his greatness
    • Proposed as answer by BRAHIM kamel Wednesday, April 08, 2009 8:15 AM
    • Marked as answer by Hikari PL Wednesday, April 08, 2009 10:59 AM
    Wednesday, April 08, 2009 8:10 AM

  • Hi,

            1. First you check it , the app.config exist in your project folder or not ?


             2. If it is exist, just open that , and compare your modifications ?


                     Still you are not getting,

            3. Build , Re-Build the application once.

            4.  Finnaly Refresh Your Connection String.

    Thank You.


              
    Srikanth
    Wednesday, April 08, 2009 8:37 AM
  • Thanks! That was the key!
    Wednesday, April 08, 2009 10:59 AM
  • After some testing I found a problem: sometimes when saving connectrionstring I encrypt/decrypt the settings. The problem is that RefreshSection does not account for that. When I start the application with encrypted settings then after decryption it breaks down (same referes to the opposite). Is there any way to prevent that?

    Furthermore please tell me why I get this additional LocalSqlServer connectrionstring?
    Wednesday, April 08, 2009 1:51 PM
  • Hikari,

    The reason you're getting LocalSqlServer is because it's probably coming from your machine.config. The machine.config, app/web.config are merged when you open the config file via the Configuration API. If you don't want this, add a <clear/> element to your <connectionStrings> section in the app/web.config file.

    Regarding your other issue, I was getting an error message complaining about the configProtectionProvider attribute that is added to the <connectionStrings> element when you encrypt. Not sure if this is the error you're getting, but if so, here's how I got around it.

    Dave
    • Marked as answer by Hikari PL Friday, April 17, 2009 9:54 AM
    Thursday, April 16, 2009 2:28 PM