app.config file

G

Guest

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.
Could someone give a hand or an example.
 
K

Kevin Spencer

Try using an "appSettings" section:

http://msdn.microsoft.com/library/d...-us/cpgenref/html/gngrfAppSettingsElement.asp
http://msdn2.microsoft.com/en-us/library/ms228154(VS.80).aspx
http://msdn2.microsoft.com/en-us/li....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.
 
P

Peter Thornqvist

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);
 
G

Guest

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>

and then, retriving it using the configmanager?
 
R

RobinS

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top