Groups | Blog | Home
all groups > dotnet general > november 2006 >

dotnet general : app.config file



HLong
11/3/2006 3:06:02 PM
Hi, I am using the app.config file to store the ConnectionString to the data.
I want to add a list of table names to this file so I could populate a combo
on FormLoad.
I found very easy to get the ConnectionString using the
ConfigurationManager.ConnectionStrings("name"), whoever I could not find any
examples of adding a list of values to the config file and retreaving it.
Kevin Spencer
11/3/2006 7:08:03 PM
Try using an "appSettings" section:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/gngrfAppSettingsElement.asp
http://msdn2.microsoft.com/en-us/library/ms228154(VS.80).aspx
http://msdn2.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings(VS.80).aspx
http://msdn2.microsoft.com/en-us/library/ms228312(VS.80).aspx

--
HTH,

Kevin Spencer
Microsoft MVP
Ministry of Software Development
http://unclechutney.blogspot.com

I just flew in from Chicago with
a man with a wooden leg named Smith
who shot an elephant in my pajamas.
So I bit him.


[quoted text, click to view]

Peter Thornqvist
11/4/2006 2:31:32 PM
You could store the items as a string separated by semi-colons. When reading
it back, you split the string and add the items to the combobox with
AddRange. Something like:

Save:

string s = ""; // use StringBuilder for better performance
foreach (string t in comboBox1.Items)
if (t != "")
s = s + t + ";";
ConfigurationManager.AppSettings["Items"] = s;

Load:

string[] ss =
ConfigurationManager.AppSettings["Items"].Split(new char[] { ';' });
comboBox1.Items.Clear();
comboBox1.Items.AddRange(ss);
--
Regards, Peter

HLong
11/6/2006 9:58:02 AM
Thanks to both Kevin and Peter for your help. Is it possible to add a custom
section something like

<test>
<add key="test1" value="Somevalue"
<add key="test2" value="Somevalue2"
</test>

Kevin Spencer
11/6/2006 6:00:27 PM
It is, but it's a bit complicated.

Here's an article that can help you get started:

http://www.codeproject.com/dotnet/customconfigsectionsNet2.asp

--
HTH,

Kevin Spencer
Microsoft MVP
Ministry of Software Development
http://unclechutney.blogspot.com

I just flew in from Chicago with
a man with a wooden leg named Smith
who shot an elephant in my pajamas.
So I bit him.


[quoted text, click to view]

RobinS
11/6/2006 8:07:42 PM
You can add strings you don't want to hardcode to the Resources. This can
be done easily by double-clicking on the project to bring up the properties,
and clicking on the <Resources> tab.

You can retrieve anything you store in here using
My.Resources.NameYouGaveIt,
where NameYouGaveIt is what you called it.

This is handy for storing stuff like messagebox strings you use a lot. I use
it to store
report headings with my company name on it, and disclosure statements that
get
printed on the bottom of reports, because they tend to change, and this way
I
only have to change them in one place.

If you're programming in C#, I'm sure there's some way to get to those
Resource
strings, but I don't know what it is.

Good luck.
Robin S.
-----------------------------------------------
[quoted text, click to view]

AddThis Social Bookmark Button