0

我正在开发一个 Windows 窗体应用程序,我让它读取一个文本文件,该文件以表格的形式存储信息。它看起来有点像这样:

ID     Name      URL
1      client1   client1.com
2      client2   client2.com
3      client3   client3.com

等等......我需要做的是从流阅读器中读取这些数据,将其放入字符串中,包括 vbtabs 和换行符,然后根据该信息创建一个数组,以便它充当之后我可以根据列名(即 ID、名称、URL)和 ID 号从中提取信息的表。我对数组没有很多经验,所以我希望在这里得到一些帮助来了解如何做到这一点。

到目前为止,我为此功能所拥有的代码是:

Dim readCLientTxtListReader As New StreamReader(strReplicationDataClientAccessListPath)
Dim strClientAccessList As String = readCLientTxtListReader.ReadToEnd

Console.Write(strClientAccessList)
readCLientTxtListReader.Close()

Dim i As Integer
Dim aryClientAccessList() As String

aryClientAccessList = strClientAccessList.Split(vbTab)
For i = 0 To UBound(aryClientAccessList)
    Console.WriteLine(aryClientAccessList)
Next i

关于这个的问题是它只是创建了一个新的数组实例作为每个 vbtab 之间的每个单独的字符串。这意味着,数组看起来像:

ID
Name
URL
1
client1
client1.com
2
client2
client2.com
3
client3
client3.com

这不是我真正需要的。

有任何想法吗?

如果您需要更多信息,请告诉我。

编辑:作为补充说明,我相信多维数组是我正在寻找的,目前正在查找它们,但如果您有更多关于这些的信息,我将不胜感激。

4

4 回答 4

0

我阅读您的最终目标的方式是,您希望拥有一个包含文件每一行的数组,并且每一行都应该是一个数组,其中包含该行的标记。如果这是正确的,那么你可以这样做:

var lines = new List<string[]>();
using (var strReplicationDataClientAccessListPath = new FileStream("path", FileMode.Open))
using (var streamReader = new StreamReader(strReplicationDataClientAccessListPath))
{
    while (streamReader.Peek() >= 0)
    {
        var line = streamReader.ReadLine();
        if (!string.IsNullOrEmpty(line))
            lines.Add(line.Split('\t'));
    }
}
foreach (var line in lines)
{
    foreach (var token in line)
        Console.Write(token);
    Console.WriteLine();
}

我很久没有做过 VB,但这里是 C#,它可以通过转换器运行以获取 VB.NET。我也使用列表来完成它,如果你最后需要一个数组,你可以这样做:

lines.ToArray();
于 2013-04-16T22:59:11.963 回答
0

这是转换为 VB 的 rclements 代码

Dim lines = New List(Of String())()
Using strReplicationDataClientAccessListPath = New FileStream("path", FileMode.Open)
  Using streamReader = New StreamReader(strReplicationDataClientAccessListPath)
    While streamReader.Peek() >= 0
      Dim line = streamReader.ReadLine()
      If Not String.IsNullOrEmpty(line) Then
        lines.Add(line.Split(ControlChars.Tab))
      End If
    End While
  End Using
End Using
For Each line As var In lines
  For Each token As var In line
    Console.Write(token)
  Next
  Console.WriteLine()
Next
于 2013-04-16T23:01:56.437 回答
0

您正在尝试模拟CSV -> DataTable工作流程,vbTab您的 CSV 分隔符在哪里,DataTable您的存储结构在哪里(您可以按字段名称和行索引查询)。互联网上有很多解决方案,只需 google 即可CSV to DataTable

这是一个,从这里链接(所以回答)。

如果您仍然想要多维数组,我建议使用 aList(Of String())而不是String(,),因为这样您就不需要管理内存分配。每行数据都是 的一个元素List和一个单维数组,其中列值是数组元素0-N

要从文件中读取,您可以使用IO.File.ReadAllLines

于 2013-04-17T11:24:27.217 回答
0

这是你需要的吗?

    'Read Values from the text file and store in a DataTable
    Dim dt As New DataTable
    Using TextReader As New IO.StreamReader("C:\Data.txt")
        Dim Line As String = TextReader.ReadLine

        If String.IsNullOrEmpty(Line) Then
            MsgBox("No Data")
            Exit Sub
        End If

        With Line.Split(vbTab)
            dt.Columns.Add(.GetValue(0))
            dt.Columns.Add(.GetValue(1))
            dt.Columns.Add(.GetValue(2))
        End With

        Do
            Line = TextReader.ReadLine
            If Line Is Nothing Then Exit Do
            dt.Rows.Add(Line.Split(vbTab))
        Loop
    End Using

    'Print the DataTable header
    For Each Column As DataColumn In dt.Columns
        Console.Write(Column.ColumnName & vbTab)
    Next

    Console.WriteLine(vbCrLf & New String("-", 24))

    'Print the DataTable contents
    For Each Row As DataRow In dt.Rows
        Console.WriteLine(Row("ID") & vbTab & Row("Name") & vbTab & Row("URL"))
    Next

我添加了另一个解决方案List,以防您更喜欢它DataTable

    'Read Values from the text file and store in a List of type Tuples
    Dim Values As New List(Of Tuple(Of String, String, String))

    Using TextReader As New IO.StreamReader("C:\Data.txt")
        Dim Line As String = TextReader.ReadLine

        Do Until Line Is Nothing
            With Line.Split(vbTab)
                Values.Add(Tuple.Create(.GetValue(0).ToString, .GetValue(1).ToString, .GetValue(2).ToString))
            End With
            Line = TextReader.ReadLine
        Loop
    End Using

    'Print the List contents
    For Each T As Tuple(Of String, String, String) In Values
        Console.WriteLine(T.Item1 & vbTab & T.Item2 & vbTab & T.Item3)
    Next
于 2014-04-17T22:50:23.047 回答