Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 373653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:19:44+00:00 2026-05-12T14:19:44+00:00

I am writing an application that has a very loosely defined config file that

  • 0

I am writing an application that has a very loosely defined config file that is read in during startup. The file is easy to parse and looks something like the following:

tag1 = value1
tag2 = value2
tag3 = value3
tag4 = value4

with tags and values being the information that a user may want to configure. As I said previously, the file is loosely defined – AKA, I don’t require all (or any) of the fields, and the values don’t have to be in any specific order. The only real rule is that it has to be in the format above, and any tags I don’t recognize get ignored.

Is there any good way to check to make sure a file is valid while parsing with the above constraints in mind? I’m developing in C#, version 2.0.x of .NET runtime. Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T14:19:45+00:00Added an answer on May 12, 2026 at 2:19 pm

    I suggest parsing the file using a regular expression into a dictionary. You can add default values for all supported keys to dictionary before reading the file. This way you get a dictionary with a consistent set of keys independent of the keys found in the file. In this case you might further consider adding a list of supported keys and checking every key against this list as you read the file reporting unsupported keys.

    const String fileName = @"foo.ini";
    
    const String entryExpression = @"^\s*(?<key>[a-zA-Z][a-zA-Z0-9]*)\s*=" + 
                                   @"\s*(?<value>[a-zA-Z][a-zA-Z0-9]*)\s*$";
    
    Dictionary<String,String> entries = new Dictionary<String, String>();
    
    foreach (String line in File.ReadAllLines(fileName))
    {
        Match match = Regex.Match(line, entryExpression);
    
        if (match.Success)
        {
            String key = match.Groups["key"].Value;
            String value = match.Groups["value"].Value;
    
            if (entries.ContainsKey(key))
            {
                Console.WriteLine("Overwriting the key '{0}'.", key);
            }
    
            entries[key] = value;
        }
        else
        {
            Console.WriteLine("Detected malformed entry '{0}'.", line);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im writing a WPF application that has a zoom and pan ability, but what
I am writing an application that runs in the background from startup to shutdown,
I'm currently in the process of writing an application that has quite a number
I'm writing an client application that has to query a URL and confirm the
I'm writing a python library that has a per-user configuration file that can be
I'm writing an audio application that has multiple threads producing sound and one thread
Im writing an application that supposed to send coordinates in an SMS, but I've
I'm writing an application that will create difficult passwords for the user. The user
I'm writing an application that interacts with other processes on a x64 Vista machine.
I'm writing an application that utilizes JavaScript timeouts and intervals to update the page.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.