foreach in a .settings file

  • Thread starter Thread starter Carlos Sosa Albert
  • Start date Start date
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>
 
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.
 
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
 
Back
Top