[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


NEWBIE: How to change app.config settings programatically?

Posted on 2006-06-06
Medium Priority
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
  • 4
LVL 52

Accepted Solution

Carl Tawn earned 1000 total points
ID: 16845738
.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

ID: 16846276

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

ID: 16846561
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

ID: 16846679
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

ID: 16851755
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.

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

873 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