none
Programmatically change app.config RRS feed

  • Question

  • I’m trying to programmatically write a connection string to app.config file using the following but I keep getting the following exception.

     

    ConfigurationErrorsException: The configuration is read only.

     

    My code:

    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString = "myconnectionstring";
    config.Save(ConfigurationSaveMode.Full);
    

     

    What am I missing here?

    • Moved by Min ZhuModerator Monday, February 14, 2011 8:17 AM (From:Windows Presentation Foundation (WPF))
    Saturday, February 12, 2011 7:58 PM

Answers

  • Hi Reality Engineering,

    Thanks for posting in the MSDN forum.

    I'm not sure whether you want to create a new connection string or modify the existing one. This code shows how to add a new one.

     private static void AddConnectionString()
        {
          // Get the application configuration file.
          System.Configuration.Configuration config =
              ConfigurationManager.OpenExeConfiguration(
              ConfigurationUserLevel.None);
    
          // Get the conectionStrings section.
          ConnectionStringsSection csSection =
            config.ConnectionStrings;
    
          //Create your connection string into a connectionStringSettings object
          ConnectionStringSettings connection = CreateNewConnectionString();
    
          //Add the object to the configuration
          csSection.ConnectionStrings.Add(connection);
    
          //Save the configuration
          config.Save(ConfigurationSaveMode.Modified);
    
          //Refresh the Section
          ConfigurationManager.RefreshSection("connectionStrings");
        }
    
    Hope it is helpful.
    Cookie Luo[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by eryang Monday, February 21, 2011 2:55 AM
    Monday, February 14, 2011 9:21 AM

All replies

  • Hi Reality Engineering,

    I have moved your post from Windows Presentation Foundation(WPF) forum to Common Language Runtime forum where you could get better and quicker support.

    Thanks for your understanding!

    Best regards,
    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, February 14, 2011 8:17 AM
    Moderator
  • Hi Reality Engineering,

    Thanks for posting in the MSDN forum.

    I'm not sure whether you want to create a new connection string or modify the existing one. This code shows how to add a new one.

     private static void AddConnectionString()
        {
          // Get the application configuration file.
          System.Configuration.Configuration config =
              ConfigurationManager.OpenExeConfiguration(
              ConfigurationUserLevel.None);
    
          // Get the conectionStrings section.
          ConnectionStringsSection csSection =
            config.ConnectionStrings;
    
          //Create your connection string into a connectionStringSettings object
          ConnectionStringSettings connection = CreateNewConnectionString();
    
          //Add the object to the configuration
          csSection.ConnectionStrings.Add(connection);
    
          //Save the configuration
          config.Save(ConfigurationSaveMode.Modified);
    
          //Refresh the Section
          ConfigurationManager.RefreshSection("connectionStrings");
        }
    
    Hope it is helpful.
    Cookie Luo[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by eryang Monday, February 21, 2011 2:55 AM
    Monday, February 14, 2011 9:21 AM