为什么我只能从我的第一个 txt 中获得结果?

Why do I only get results from my first txt?

我正在尝试读取和使用 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 reader (我在 Whosebug 上找到了这里):

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

示例代码有几个问题,我不确定是什么导致了实际问题。

  1. 不要编写自己的 ReadAllLinesAsync,只需使用 File.ReadAllLinesAsync
  2. 您通常应该避免 .Result,因为这是一个阻塞操作,并且有可能导致死锁。在这种情况下,您应该只调用同步版本 File.ReadAllLines
  3. 如果可能,请使用 foreach 或 for 循环。它们使您更容易查看您的代码是否正确,并避免将循环逻辑分散到多行。
  4. 据我所知,您正在以相同方式处理每一行,因此您应该能够使用 List<string> linesAddRange
  5. 合并所有文件中的所有行
  6. 与其保留 15 个不同的属性列表,更常见的方法是存储具有 15 个不同属性的 class 的一个对象列表。
  7. 每当您需要存储数据时,您应该认真考虑使用现有的序列化格式,例如 json、xml、csv、protobuf 等。这样您就可以使用现有的、经过良好测试的库用于写入和读取数据并将其转换为您自己的类型。