foreach in a .settings file

C

Carlos Sosa Albert

Hi guys,

Is there a way to use a foreach (C#) to go through a settings file like
this?

I can't find a collection in MyProject.MyConfigurationFile.settings.xxx, I
cant only access the settings refering like
MyProject.MyConfigurationFile.settings.Default.key1,
MyProject.MyConfigurationFile.settings.Default.key2, etc.

Thanks a lot!!!

settings file:
_______________________________________________
<?xml version='1.0' encoding='utf-8'?>
<SettingsFile
xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings"
CurrentProfile="(Default)" GeneratedClassNamespace="CoachLibrary"
GeneratedClassName="InitialConfiguration">
<Profiles />
<Settings>
<Setting Name="key1" Type="System.String" Scope="Application">
<Value Profile="(Default)">abc</Value>
</Setting>
<Setting Name="key2" Type="System.String" Scope="Application">
<Value Profile="(Default)">xyz</Value>
</Setting>
</Settings>
</SettingsFile>
 
B

Bob Powell [MVP]

Open the file in an XmlDocument and walk the list of nodes in the doc?

Use XPath to to a Select on the nodes and a for-each to step through the
ones you get back?


--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
G

geoffrey.munday

My first thought was what Bob suggested but you can try something like
the following as well (I'm not expressing any warranties with this
approach, you will need to tweak it and see what works for you).

foreach (System.Configuration.SettingsProperty property in
MyProjectNamespace.Properties.Settings.Default.Properties)
{
switch (property.Name)
{
case "key1":
string key1 = (string)property.DefaultValue;
//use key1
break;
case "key2":
string key2 = (string)property.DefaultValue;
//use key2
break;
}
}

Good luck!
-Geoff
 

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