我正在尝试遍历文本文件中的数据并将其附加到列表视图

I am trying to loop through my data in text file and append it to list view

我的文本文件看起来像这样。每个值都在新行上,我想遍历它并将其显示到我的列表视图中。 我想为一个列表视图行循环 7 行

husnain
zahid
john
UET society
lahore
22
Thursday April 14 2022
00000000
uzair
ejaz
doe
nasheman iqbal phase 2
 lahore
27
2022 ,اپریل 16
000000

我正在使用这个循环遍历文件并将其保存到我的列表视图中

 private void LoadDataToListview()
    {
        var fileLines = File.ReadAllLines(@"E:\SCD_Project\Theory Assignments\A2\WindowsFormsApp1\WindowsFormsApp1\addressBook.txt");
        lineval = fileLines.Length;
        
        
        for (int i = 1; i < 16; i += 8)
        {
            S3_manageAddress_listview.Items.Add(
                 new ListViewItem(new[]
                 {
                        fileLines[i],
                        fileLines[i + 1],
                        fileLines[i + 2],
                        fileLines[i + 3],
                        fileLines[i + 4],
                        fileLines[i + 5],
                        fileLines[i + 6],
                        fileLines[i + 7],
     }));          
        }       
    }

C# 中的数组是从零开始的,因此您需要从 0 而不是 1 开始迭代。

private void LoadDataToListview()
{
    var fileLines = File.ReadAllLines(@"E:\SCD_Project\Theory Assignments\A2\WindowsFormsApp1\WindowsFormsApp1\addressBook.txt");
    var groupSize = 8;
    //lineval = fileLines.Length;
   
    // start iterating at 0
    for (int i = 0; i < fileLines.Length; i += groupSize)
    {
        S3_manageAddress_listview.Items.Add(
            new ListViewItem(new[]
            {
                fileLines[i],
                fileLines[i + 1],
                fileLines[i + 2],
                fileLines[i + 3],
                fileLines[i + 4],
                fileLines[i + 5],
                fileLines[i + 6],
                fileLines[i + 7],
        }));
    }
}