NEWBIE: How to change app.config settings programatically?

Posted on 2006-06-06
Last Modified: 2012-05-05
Dear Experts,

I would like to allow my end-user to enter a value through my UI, and then place that value in an app.config.  Specifically, I would like the user to type in a value for "MYDATABASE", and insert it below.

This is my app.config:

<?xml version="1.0" encoding="utf-8" ?>
        <add name="ParmViewerCore.Properties.Settings.MyConnectionString"
            connectionString="Data Source=MyBox;Initial Catalog=MYDATABASE;Persist Security Info=True;User ID=myUser;Password=myPassword"
            providerName="System.Data.SqlClient" />

What is the best way to do this with C#?  (I'm on VS and MS SQL 2005).

BTW: Is this a bad thing to do?  I don't want my users to have to figure out how to change app.config itself--given that I don't even know how yet...

Question by:BrianMc1958
    LVL 52

    Accepted Solution

    .Net 1.0 or 2.0 ?

    With 1.0 the .Net classes for reading the config file are read-only, so the only way to modify it directly is to open it as an XML file and manipulate it that way. Not sure if this is still the case for 2.0.

    Author Comment


    Although it seems like a nice way to configure the app if you have willing technical people on the other end, it seems hopelessly complex for ordinary office workers to be asked to edit XML in order to configure an simple (non-Enterprise...) app.

    Is there a user-friendly way to do this?  Is there some sort of "app.config UI" that's available on the user's end after publishing, for instance?


    Author Comment

    I have since found this:

                Properties.Settings set = new Properties.Settings();
                string str = set.myConnectionString;
                set.myConnectionString = "Some New String...";

    However (and this is getting frustrating), it DOESN'T WORK.  The compiler says the field is read-only.  (The site I found this at mentions no such thing...)

    Any more ideas?

    Author Comment

    Have since found out it needs to be a user setting, not an app setting.  Now changing the wall against which I am banging my head...

    Author Comment

    For anyone reading this, it appears ConnectionString can only be an app setting, and therefore cannot be modified by the user.  As this question has now twisted so far from it's origin, I'll award points and post another question...

    Thanks, everybody.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Hire Top Freelancers to Complete C# Projects

    Source the talented Expert Exchange community
    for top quality work on your C# projects.

    Hire the best. Collaborate easily. Get quality work.

    Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now