相关疑难解决方法(0)

C#Linq .ToDictionary()密钥已经存在

最终编辑:我能够找到ini文件中的重复字段.谢谢大家的帮助!

我正在使用正则表达式来解析ini文件,使用LINQ将其存储在Dictionary中:

示例数据:
[WindowSettings]
窗口X Pos ='0'
窗口Y Pos ='0'
窗口最大化='false'
窗口名称='Jabberwocky'

[记录]
目录='C:\ Rosetta Stone\Logs'

编辑:这是实际导致问题的文件:http://pastebin.com/mQSrkrcP

EDIT2:我把它缩小到由文件的最后一部分引起:[list_first_nonprintable]

由于某种原因,我正在解析的文件之一是抛出此异常:

System.ArgumentException:已添加具有相同键的项.

有没有办法让我找出导致问题的键(所以我可以修复文件),或者只是跳过导致这个问题的键并继续解析?

这是代码:

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)) …
Run Code Online (Sandbox Code Playgroud)

c# regex linq ini dictionary

5
推荐指数
1
解决办法
1万
查看次数

标签 统计

c# ×1

dictionary ×1

ini ×1

linq ×1

regex ×1