Der Schlüssel C # Linq .ToDictionary () ist bereits vorhanden

Letzte Änderung: Ich konnte das doppelte Feld in der INI-Datei finden. Vielen Dank für Ihre Hilfe an alle!

Ich verwende einen regulären Ausdruck, um eine INI-Datei zu analysieren, und LINQ, um sie in einem Dictionary zu speichern:

Beispieldaten:
[WindowSettings]
Fenster X Pos = '0'
Fenster Y Pos = '0'
Fenster maximiert = 'false'
Fenstername = 'Jabberwocky'

[Protokollierung]
Verzeichnis = 'C: \ Rosetta Stone \ Logs'

BEARBEITEN: Hier ist die Datei, die das Problem tatsächlich verursacht:http://pastebin.com/mQSrkrcP

EDIT2: Ich habe es auf den letzten Abschnitt in der Datei zurückgeführt: [list_first_nonprintable]

Aus irgendeinem Grund löst eine der Dateien, mit denen ich das analysiere, diese Ausnahme aus:

System.ArgumentException: Ein Element mit demselben Schlüssel wurde bereits hinzugefügt.

Gibt es eine Möglichkeit für mich, herauszufinden, welcher Schlüssel das Problem verursacht (damit ich die Datei reparieren kann), oder einfach den Schlüssel zu überspringen, der dies verursacht, und das Parsen fortzusetzen?

Hier ist der 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>>();
}

Danke im Voraus.

Antworten auf die Frage(3)

Ihre Antwort auf die Frage