Results 1 to 4 of 4

Thread: [2.0] how to read the contents of app.config file in a loop?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2003
    Posts
    436

    [2.0] how to read the contents of app.config file in a loop?

    I want to read the all elements under <appSettings> tag in a loop.

    this is my app.config file

    Code:
    <appSettings>
    
    <add key="SourceFile_Advances" value="C:\Code\SavvysoftValuations\MV ADVS.csv" />
    
    <add key="DestinationFile_Advances" value="C:\Code\SavvysoftValations\Destination\MV ADVS.csv"/>
    
    <add key="SourceFile_aaa" value="C:\Code\SavvysoftValuations\MV SWPS.csv" />
    
    <add key="DestinationFile_aaa" value="C:\Code\SavvysoftValations\Destination\MV SWPS.csv"/>
    
    </appSettings>
    currently I am using the following code to read the elements . but I would like to read the elements (under appsettings) in a loop . If in future if new elements are added or deleted under appSettings tag, I don't want to make any changes to my application. Basically, instead of hard coding the element name, I would like to read all the elements under <appSettings> in a loop. How can I do that ? I can load the appSettings into an XMLDocument object and traverse but I was wondering if there is any better wa.

    Code:
    _valuations_Advances_sourceFile = System.Configuration.ConfigurationSettings.AppSettings["SourceFile_Advances"];
    
    _valuations_advances_destinationFile = System.Configuration.ConfigurationSettings.AppSettings["DestinationFile_Advances"];

  2. #2
    Fanatic Member BillBoeBaggins's Avatar
    Join Date
    Jan 2003
    Location
    in your database, dropping your tables.
    Posts
    628

    Re: [2.0] how to read the contents of app.config file in a loop?


  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] how to read the contents of app.config file in a loop?

    AppSettings is type NameValueCollection. Use a For Each loop like you do any other collection. Also, ConfigurationSettings.AppSettings is obsolete in .NET 2.0, which I believe the IDE should have told you. You should be using ConfigurationManager.AppSettings in .NET 2.0 if I'm not mistaken.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Fanatic Member popskie's Avatar
    Join Date
    Jul 2005
    Location
    In my chair
    Posts
    666

    Re: [2.0] how to read the contents of app.config file in a loop?

    VB Code:
    1. NameValueCollection nvc = ConfigurationManager.AppSettings;
    2.            
    3.            foreach (string tete in nvc.AllKeys)
    4.             {
    5.                 System.Console.WriteLine(nvc[tete]);
    6.             }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width