【发布时间】:2022-01-10 11:16:28
【问题描述】:
我正在尝试读取和使用 txt 文件中的所有行。使用一种方法,我遍历它们 asinc,并尝试获取数据。我的问题是,输出看起来只包含第一个 txt 文件中的数据。我只是找不到问题所在。我将不胜感激。
这是我的代码:
string[] files = Directory.GetFiles("C:/DPS-EDPWB05/forlogsearch", "*", SearchOption.AllDirectories);
//data I need from the txts
List<string> num = new List<string>();
List<string> date = new List<string>();
List<string> time = new List<string>();
List<string> sip = new List<string>();
List<string> csmethod = new List<string>();
List<string> csuristem = new List<string>();
List<string> csuriquery = new List<string>();
List<string> sport = new List<string>();
List<string> csusername = new List<string>();
List<string> cip = new List<string>();
List<string> csuseragent = new List<string>();
List<string> csreferer = new List<string>();
List<string> scstatus = new List<string>();
List<string> scsubstatus = new List<string>();
List<string> cswin32status = new List<string>();
List<string> timetaken = new List<string>();
int x = 0;
int y = 0;
int i = 0;
int filesCount = 0;
string v = "";
//Taking the data from the Log, getting a list of string[]
//items with the lines from the txts
List<string[]> lines = new List<string[]>();
while (i < files.Length)
{
lines.Add(ReadAllLinesAsync(files[i]).Result);
i++;
}
//Trying to get the data from the string[]s
do
{
string line;
int f = 0;
string[] linesOfTxt = lines[filesCount];
do
{
line = linesOfTxt[f];
string[] splittedLine = { };
splittedLine = line.Split(' ', 15, StringSplitOptions.None);
y = splittedLine.Count();
if (y == 15)
{
num.Add(x.ToString());
date.Add(splittedLine[0]);
time.Add(splittedLine[1]);
sip.Add(splittedLine[2]);
csmethod.Add(splittedLine[3]);
csuristem.Add(splittedLine[4]);
csuriquery.Add(splittedLine[5]);
sport.Add(splittedLine[6]);
csusername.Add(splittedLine[7]);
cip.Add(splittedLine[8]);
csuseragent.Add(splittedLine[9]);
csreferer.Add(splittedLine[10]);
scstatus.Add(splittedLine[11]);
scsubstatus.Add(splittedLine[12]);
cswin32status.Add(splittedLine[13]);
timetaken.Add(splittedLine[14]);
x++;
}
f++;
} while (f < linesOfTxt.Length);
filesCount++;
}
while (filesCount < files.Count());
在这一切之后,我对这些和东西进行了分组,但这是在我需要的数据列表被填满之后发生的——所以问题一定出在某个地方。另外,我的 asinc 阅读器(我在 stackoverflow 上找到):
public static Task<string[]> ReadAllLinesAsync(string path)
{
return ReadAllLinesAsync(path, Encoding.UTF8);
}
public static async Task<string[]> ReadAllLinesAsync(string path, Encoding encoding)
{
var lines = new List<string>();
// Open the FileStream with the same FileMode, FileAccess
// and FileShare as a call to File.OpenText would've done.
using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, DefaultBufferSize, DefaultOptions))
using (var reader = new StreamReader(stream, encoding))
{
string line;
while ((line = await reader.ReadLineAsync()) != null)
{
lines.Add(line);
}
}
return lines.ToArray();
}
【问题讨论】:
-
这里为什么需要
async?无论哪种方式,您都不会在阅读.Result...之前等待异步任务完成... -
(另外,你的数据模型看起来很奇怪——为什么有十几个字符串数组而不是一个包含所有这些字段的结构/类数组?)
-
你的数据模型看起来很奇怪 - “使用 N 个数组,其中的数据按位置关联”表示程序程序员来到 OO,恕我直言
-
感谢您的 cmets。首先,我尝试了异步,因为我的其他方法(顺便说一句效果很好)太慢了。这种方法解决了阅读速度慢的问题,但现在我需要弄清楚另一个问题。其次,关于数据模型和其他东西:我是 C# 新手,我正在尝试获得一些经验 - 认为这个项目将是一个很好的机会。现在我有一个我无法解决的问题,我的大脑也纠结于此,所以我不能离开它...... :)
-
@AKX ReadAllLinesAsync 确实在等待,我认为这就足够了。
标签: c# asynchronous readline txt