【发布时间】:2016-05-01 08:58:16
【问题描述】:
我有一个应用程序可以加载格式为key:value 的文件,然后将其添加到Dictionary。这适用于小文件,但是当我尝试加载一个有 65,000 行的文件时,它不起作用并在Dictionary.TryAdd() 上抛出Index was outside the bounds of the array.。
我为 64 位架构编译我的应用程序,我还在 app.config 中设置了 <gcAllowVeryLargeObjects enabled="true" />。
private void LoadFile()
{
ConcurrentDictionary<string, string> Dict = new ConcurrentDictionary<string, string>();
OpenFileDialog dlgFile = new OpenFileDialog();
dlgFile.Filter = "All Files (*.*)|*.*";
dlgFile.FilterIndex = 1;
if (dlgFile.ShowDialog() == DialogResult.OK)
{
foreach (string line in File.ReadLines(dlgFile.FileName))
{
// Index was outside the bounds of the array.
Dict.TryAdd(line.Split(':')[0], line.Split(':')[1]);
}
}
}
【问题讨论】:
-
我认为问题出在数组索引上。你确定所有的“行”都符合要求的格式,也就是可以用':'分割吗?
-
@Daniel Leszen 是的,他们是
-
我建议你再检查一次。抛出异常时,将鼠标悬停在
line上以查看失败的值是什么。很可能它不包含:- 如果您确定它们都包含,那么它有时是最后(空)行。 -
@J.Ivanovic 通用的
Dictionary类没有TryAdd()方法——你使用的是哪个类? -
如何跳过空行?
标签: c# file dictionary