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 7948155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:38:28+00:00 2026-06-04T01:38:28+00:00

Final Edit: I was able to locate the duplicate field in the ini file.

  • 0

Final Edit: I was able to locate the duplicate field in the ini file. Thanks for your help everyone!

I’m using a regular expression to parse an ini file and LINQ to store it in a Dictionary:

Sample Data:
[WindowSettings]
Window X Pos=’0′
Window Y Pos=’0′
Window Maximized=’false’
Window Name=’Jabberwocky’

[Logging]
Directory=’C:\Rosetta Stone\Logs’

EDIT: Here is the file actually causing the problem: http://pastebin.com/mQSrkrcP

EDIT2: I’ve narrowed it down to being caused by the last section in the file: [list_first_nonprintable]

For some reason one of the files that I’m parsing with this is throwing this exception:

System.ArgumentException: An item with the same key has already been added.

Is there any way for me to either find out which key is causing the problem (so I can fix the file), or to just skip the key that’s causing this and continue parsing?

Here is the code:

try
{
    // Read content of ini file.
    string data = System.IO.File.ReadAllText(project);

    // Create regular expression to parse ini file.
    string pattern = @"^((?:\[)(?<Section>[^\]]*)(?:\])(?:[\r\n]{0,}|\Z))((?!\[)(?<Key>[^=]*?)(?:=)(?<Value>[^\r\n]*)(?:[\r\n]{0,4}))*";
    //pattern = @"
    //^                           # Beginning of the line
    //((?:\[)                     # Section Start
    //     (?<Section>[^\]]*)     # Actual Section text into Section Group
    // (?:\])                     # Section End then EOL/EOB
    // (?:[\r\n]{0,}|\Z))         # Match but don't capture the CRLF or EOB
    // (                          # Begin capture groups (Key Value Pairs)
    //  (?!\[)                    # Stop capture groups if a [ is found; new section
    //  (?<Key>[^=]*?)            # Any text before the =, matched few as possible
    //  (?:=)                     # Get the = now
    //  (?<Value>[^\r\n]*)        # Get everything that is not an Line Changes
    //  (?:[\r\n]{0,4})           # MBDC \r\n
    //  )*                        # End Capture groups";

    // Parse each file into a Dictionary.
    Dictionary<string, Dictionary<string, string>> iniFile
                    = (from Match m in Regex.Matches(data, pattern, RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline)
                       select new
                       {
                           Section = m.Groups["Section"].Value,

                           kvps = (from cpKey in m.Groups["Key"].Captures.Cast<Capture>().Select((a, i) => new { a.Value, i })
                                   join cpValue in m.Groups["Value"].Captures.Cast<Capture>().Select((b, i) => new { b.Value, i }) on cpKey.i equals cpValue.i
                                   select new KeyValuePair<string, string>(cpKey.Value, cpValue.Value)).ToDictionary(kvp => kvp.Key, kvp => kvp.Value)

                       }).ToDictionary(itm => itm.Section, itm => itm.kvps);

    return iniFile;
}
catch (ArgumentException ex)
{
    System.Diagnostics.Debug.Write(ex.ToString());
    return new Dictionary<string, Dictionary<string, string>>();
}

Thanks in advance.

  • 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-06-04T01:38:29+00:00Added an answer on June 4, 2026 at 1:38 am

    This just means that when you convert to a Dictionary —

    .ToDictionary(itm => itm.Section, itm => itm.kvps);
    

    — there are multiple keys (itm.Section). You can use ToLookup instead, which is kind of like a dictionary but allows multiple keys.

    Edit

    There are a couple of ways to call ToLookup. The simplest is to specify the key selector:

    var lookup = 
       // ...
    .ToLookup(itm => itm.Section);
    

    This should provide a lookup where the key is of type Group. Getting a lookup value should then return an IEnumerable, where T is the anonymous type:

    Group g = null;
    // TODO get group
    var lookupvalues = lookup[g];
    

    If the .NET compiler doesn’t like this (sometimes it seems to have trouble figuring out what the various types should be), you can also specify an element selector, for example:

    ILookup<string, KeyValuePair<string,string>> lookup = 
        // ...
    .ToLookup(
        itm => itm.Section.Value,    // key selector
        itm => itm.kvps              // element selector
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Edit: With enough rewriting, and commenting, I have it running, will post final below
The final images produced by compliers contain both bin file and extended loader format
I am using the following code. .ascx file: <div class=DemoArea> <asp:Button ID=btnCaseComplete runat=server Text=Case
I'm trying to extract only certain elements of a string using regular expressions and
EDIT The final goal is to call a stored procedure hosted in sybase with
Howdy, using vs2008 winforms. I want to be able to use a slightly customised
I am using seam 2.2.2.Final on JBoss AS 5. I am working on a
final ProgressDialog Pdialog = ProgressDialog.show(SpinnerClass.this, , Loading. Please wait..., true); Thread ProgressThread = new
final String message[] = {,,}; try{ String UID = null, UBAL = null; DateFormat
Can final keyword be used for a method?

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.