locked
write to app.config at runtime (very urgent) RRS feed

  • Question

  • hello
                I want to write to app.config at runtime   i want to change name and connectionString 
    <add name="ConnectionString" connectionString="Data Source=mydb2;User Id=123;Password=123"
    providerName="System.Data.OracleClient" />

    How can i do this

    It's Me
    Tuesday, June 23, 2009 11:53 AM

Answers

  • Hi,

    To make things clear I need to state that at runtime you can not actually edit app.config because after application has been deployed the app.config's content will be copied to Application.exe.config under you application directory.

    Therefore your application will have nothing to do with app.config any more. At runtime if you want to modify the configuratiion file the only way is to do with Application.exe.config.

    To modify the Application.exe.config you need to use ConfigurationManager class.
    Here is the code sample:
       1: // Open App.Config of executable
       2: System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
       3: // Add an Application Setting.
       4: config.AppSettings.Settings.Remove("LastDateFeesChecked");
       5: config.AppSettings.Settings.Add("LastDateFeesChecked", DateTime.Now.ToShortDateString());
       6: // Save the configuration file.
       7: config.Save(ConfigurationSaveMode.Modified);
       8: // Force a reload of a changed section.
       9: ConfigurationManager.RefreshSection("appSettings");

    Thanks
    Binze
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Bin-ze Zhao Thursday, July 2, 2009 2:37 AM
    • Unmarked as answer by It_s Meee Friday, July 3, 2009 5:02 AM
    • Proposed as answer by Kenar716 Wednesday, May 7, 2014 6:49 PM
    • Marked as answer by Gaurav Aroraa Monday, March 13, 2017 3:35 AM
    Tuesday, June 30, 2009 10:37 AM

All replies

  • Hi,

    Configuration is a read-only object. You can't dynamically edit any thing.
    In fact you can read all the things of the Configuration by using System.Configuration namespace.

    Ex. To read the ConnectionString

    ConnectionStringSettings connString = ConfigurationManager.ConnectionStrings["ConnectionString"];

    Regards
    Dnana

    • Marked as answer by Bin-ze Zhao Saturday, June 27, 2009 9:30 AM
    • Unmarked as answer by It_s Meee Tuesday, June 30, 2009 9:36 AM
    Tuesday, June 23, 2009 12:20 PM
  • not i think we can but how to do that i still wounder
    It's Me
    Thursday, June 25, 2009 5:40 AM
  • Hi,

    To make things clear I need to state that at runtime you can not actually edit app.config because after application has been deployed the app.config's content will be copied to Application.exe.config under you application directory.

    Therefore your application will have nothing to do with app.config any more. At runtime if you want to modify the configuratiion file the only way is to do with Application.exe.config.

    To modify the Application.exe.config you need to use ConfigurationManager class.
    Here is the code sample:
       1: // Open App.Config of executable
       2: System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
       3: // Add an Application Setting.
       4: config.AppSettings.Settings.Remove("LastDateFeesChecked");
       5: config.AppSettings.Settings.Add("LastDateFeesChecked", DateTime.Now.ToShortDateString());
       6: // Save the configuration file.
       7: config.Save(ConfigurationSaveMode.Modified);
       8: // Force a reload of a changed section.
       9: ConfigurationManager.RefreshSection("appSettings");

    Thanks
    Binze
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Bin-ze Zhao Thursday, July 2, 2009 2:37 AM
    • Unmarked as answer by It_s Meee Friday, July 3, 2009 5:02 AM
    • Proposed as answer by Kenar716 Wednesday, May 7, 2014 6:49 PM
    • Marked as answer by Gaurav Aroraa Monday, March 13, 2017 3:35 AM
    Tuesday, June 30, 2009 10:37 AM
  • Hello

              i run the application and open  config.exe file . it does not change  see the code and config below and plz help me to solve this issue
     
    <?xml version="1.0" encoding="utf-8" ?>

    <

     

    configuration>

    <

     

    configSections>

    <

     

    section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />

    </

     

    configSections>

    <

     

    dataConfiguration defaultDatabase="ConnectionString" />

    <

     

    connectionStrings>

    <

     

    add name="ConnectionString" connectionString="Data Source=mydb;User Id=123;Password=123"

     

     

    providerName="System.Data.OracleClient" />

    </

     

    connectionStrings>

     

    </

     

    configuration>

    code as follows


    System.Configuration.

    Configuration config2 = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

     

    // Add an Application Setting.

     

    // config.AppSettings.Settings.Remove("LastDateFeesChecked");

     

    ConnectionStringSettings st = new ConnectionStringSettings("ConnectionString", "Data Source=mydb;User Id=123;Password=123", "System.Data.OracleClient");

    config2.ConnectionStrings.ConnectionStrings.Remove(st);

     

    ConnectionStringSettings st2 = new ConnectionStringSettings("ConnectionString", "Data Source=mydb;User Id=788;Password=788", "System.Data.OracleClient");

    config2.ConnectionStrings.ConnectionStrings.Add(st2);

     

    // Save the configuration file.

    config2.Save(

    ConfigurationSaveMode.Modified);

     

    // Force a reload of a changed section.

     

    ConfigurationManager.RefreshSection("ConnectionString");


    It's Me
    Friday, July 3, 2009 5:06 AM
  • hello
              well i have following file in debug

    configproject.exe.Config

    configproject.vshost.exe.Config

    when i run the application i changes to "configproject.vshost.exe.Config" file and when i stop the application . it move back to it pervious state

    for example befor runiing application ( see the userid an

    add name="ConnectionString" connectionString="Data Source=mydb;User Id=456;Password=456"providerName="System.Data.OracleClient" />

    i run the application it change the config file as requried ( see the userid an password has change


    add name="ConnectionString" connectionString="Data Source=mydb;User Id=123;Password=123"providerName="System.Data.OracleClient" />

    after i stop the application it move to pervious state
    add name="ConnectionString" connectionString="Data Source=mydb;User Id=456;Password=456"providerName="System.Data.OracleClient" />

    regards


    It's Me
    Friday, July 3, 2009 5:41 AM
  • Hi There you are probably running your app in the debugger.
    All the changes you make are made in the .exe.config and not app.config

    Every time you compile the .exe.config file is overwritten by app.config

    Just run your app from windows explorer and the values will keep their settings.

    Wednesday, July 22, 2009 12:13 PM
  • This has been answered here:

    http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/b865ce7a-6616-4109-90a5-553efc928075
    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    Wednesday, July 22, 2009 1:39 PM
  • I try to config.AppSettings.Settings["key"].Value = value

    and configuration save nothing to my app.config.

    .net framework 4.6

    Saturday, March 4, 2017 11:42 AM