I am trying to read the text file to check whether all the rows has same number of columns or not. In local code its working fine but on the Network shared folder (has permission as Everyone) it is working only for small size (5mb) of file and when I am selecting 10 MB or 500 MB file same code is not working (Not working means, it takes some time but after few minutes page gets refresh, that's it). It is not giving any error or showing any message. Below is the code to read the file and get the columns count
我正在尝试读取文本文件以检查所有行是否具有相同的列数。在本地代码中,它的工作正常,但在网络共享文件夹(具有Everyone权限)它只适用于小尺寸(5mb)的文件,当我选择10 MB或500 MB文件相同的代码不工作(不工作意味着,它需要一些时间,但几分钟后页面会刷新,就是这样)。它没有给出任何错误或显示任何消息。下面是读取文件并获取列数的代码
LinesLst = File.ReadLines(_fileName, Encoding.UTF8)
.Select((line, index) =>
{
var count = line.Split(Delimiter).Length;
if (NumberOfColumns < 0)
NumberOfColumns = count;
return new
{
line = line,
count = count,
index = index
};
})
.Where(colCount => colCount.count != NumberOfColumns)
.Select(colCount => colCount.line).ToList();
2 个解决方案
#1
0
Perhaps you have OutOfMemoryException
on large file. The fact is that are created many objects in the code on each iteration: the string array by line.Split
and an anonymous object. Meanwhile, the anonymous object in fact is not needed. I would rewrote the code as so:
也许你在大文件上有OutOfMemoryException。事实是,在每次迭代的代码中创建了许多对象:line.Split的字符串数组和匿名对象。同时,实际上不需要匿名对象。我会重写代码:
LinesLst = File.ReadLines(_fileName, Encoding.UTF8)
.Where(line =>
{
var count = line.Split(Delimiter).Length;
if (NumberOfColumns < 0)
NumberOfColumns = count;
return count != NumberOfColumns;
})
.ToList();
In addition, you can try to get rid of the creation of the string array when you call the line.Split
. Try to replace the string
此外,您可以尝试在调用line.Split时摆脱字符串数组的创建。尝试替换字符串
var count = line.Split(Delimiter).Length;
to the string
到字符串
// Assume that Delimiter is char[]
var count = line.Count(c => Delimiter.Contains(c)) + 1;
// Assume that Delimiter is char
var count = line.Count(c => Delimiter == c) + 1;
#2
0
I have added AsyncPostBackTimeout="36000" which solved my problem.
我添加了AsyncPostBackTimeout =“36000”,这解决了我的问题。
#1
0
Perhaps you have OutOfMemoryException
on large file. The fact is that are created many objects in the code on each iteration: the string array by line.Split
and an anonymous object. Meanwhile, the anonymous object in fact is not needed. I would rewrote the code as so:
也许你在大文件上有OutOfMemoryException。事实是,在每次迭代的代码中创建了许多对象:line.Split的字符串数组和匿名对象。同时,实际上不需要匿名对象。我会重写代码:
LinesLst = File.ReadLines(_fileName, Encoding.UTF8)
.Where(line =>
{
var count = line.Split(Delimiter).Length;
if (NumberOfColumns < 0)
NumberOfColumns = count;
return count != NumberOfColumns;
})
.ToList();
In addition, you can try to get rid of the creation of the string array when you call the line.Split
. Try to replace the string
此外,您可以尝试在调用line.Split时摆脱字符串数组的创建。尝试替换字符串
var count = line.Split(Delimiter).Length;
to the string
到字符串
// Assume that Delimiter is char[]
var count = line.Count(c => Delimiter.Contains(c)) + 1;
// Assume that Delimiter is char
var count = line.Count(c => Delimiter == c) + 1;
#2
0
I have added AsyncPostBackTimeout="36000" which solved my problem.
我添加了AsyncPostBackTimeout =“36000”,这解决了我的问题。