none
Deleting a connection string from app.config file programmatically?How do you do it? RRS feed

  • Question

  • Hi,
    I need to provide a small tool app where i need to show all connection string and give the ability to add -edit and delete a connection string from the app.config file.

    How do you delete or amend a connection string programmatically?
    Any example anywhere?

    Thanks a lot
    Thanks for your help
    Saturday, December 5, 2009 5:55 AM

Answers

  • Hi,

    If you want to change config file at runtime that means you'll modify the config at the folder that your executable is. Changing source is a total different issue, think that when you publish the project there will be no source.
    • Marked as answer by devBrix Saturday, December 5, 2009 1:12 PM
    Saturday, December 5, 2009 1:09 PM

All replies

  • Hi,

    You can use the following code.

    But it deletes from the config file in output folder(debug or release folder), doesn't changes the app.config in source.

    You need to add System.Configuration as a reference to your project.

                Configuration c = System.Configuration.ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
                c.ConnectionStrings.ConnectionStrings.Remove("MyConn");
                c.Save();
    Saturday, December 5, 2009 6:22 AM
  • thanks for your code I will give it a try.
    Could you tell me why it doenst change the app.config in source?
    Is it because you are pointing at application.ExecutablePath and depending if you are in debug or release it will change it there.Is this correct?
    Is it possible to change source as well?
    What about amending it?


    thanks again for your time.Sorry for the extra questions
    Thanks for your help
    Saturday, December 5, 2009 1:04 PM
  • Hi,

    If you want to change config file at runtime that means you'll modify the config at the folder that your executable is. Changing source is a total different issue, think that when you publish the project there will be no source.
    • Marked as answer by devBrix Saturday, December 5, 2009 1:12 PM
    Saturday, December 5, 2009 1:09 PM
  • True when i publish there will be no source.Thanks a lot
    That worked
    Thanks for your help
    Saturday, December 5, 2009 1:12 PM
  • Hi,
    1. Please add a property named "ConnectionsStringsInXML" to your project properties.
    2. Write your connection strings by XMLWriter on that property e.g.
    Properties.Settings.Default[ConnectionsStringsInXML] = yourXMLWriter.ToString();
    3. Update your config file i.e.
    Properties.Settings.Default.Save();


    With best regards, Yasser Zamani

    Saturday, December 5, 2009 1:14 PM