CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2010
    Posts
    15

    Reading a properties file

    I am looking for a better method to read a properties file. My program loads and does a loop with some processing based on a properties file. The properties file can be changed on the fly to change how the loop runs or what processing is performed.

    Here is what I do right now:

    The config file has values like this:

    source=c:\data\data.txt

    Code:
    string[] configProp = File.ReadAllLines(@"config.properties");
    I load the file into a string array. I then proceed to reassign string array value into a new string and perform trims to remove the leader "source=" and I am left with "c:\data\data.txt" which is my usable variable.

    This just seems like a lot of extra work, does anyone know of a better way to load a properties file?

  2. #2
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: Reading a properties file

    Try this:
    Code:
    IEnumerable<KeyValuePair<String, String>> properties = from p in File.ReadAllLines(@"config.properties")
            select new KeyValuePair<String, String>( 
                p.Split(new Char[] { '=' })[0], 
                p.Split(new Char[] { '=' })[1]);
    
    foreach(KeyValuePair<String, String> property in properties)
    {
         ...
    }
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  3. #3
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Reading a properties file

    Is properties file a must, or can you use other format, e.g. .NET"s common config file? If so, there are plenty classes in System.Configuration namespace which can help you.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

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

Featured