1

我尝试使用Directory.Getfiles,但它不会获取目录中的所有文件。文件为 .txt 格式这是我的代码Form_Load

Public SourceDir As String = Application.StartupPath & "\Source\"
If Not Directory.Exists(SourceDir) Then
        Directory.CreateDirectory(SourceDir)
    End If
Dim Files() As String = Directory.GetFiles(SourceDir )


    For Each File In Files
        Dim Lines() As String
        Dim StockList As List(Of ListViewItem) = New List(Of ListViewItem)
        Dim StockItem As ListViewItem
        MsgBox(File)

        Lines = IO.File.ReadAllText(File).Split(New String() {ControlChars.CrLf}, StringSplitOptions.RemoveEmptyEntries)

        For j = 0 To Lines.Length - 1 Step 8

            StockItem = New ListViewItem(Lines(j))
            StockItem.SubItems.Add(Lines(j + 1))
            StockItem.SubItems.Add(Lines(j + 2))
            StockItem.SubItems.Add(Lines(j + 3))
            StockItem.SubItems.Add(Lines(j + 4))
            StockItem.SubItems.Add(Lines(j + 5))
            StockItem.SubItems.Add(Lines(j + 6))
            StockItem.SubItems.Add(Lines(j + 7))
            listHouse.Items.Add(StockItem)
        Next
    Next

它只显示其中一个文件,仅此而已!

4

2 回答 2

0

您可能需要SearchOption.AllDirectories使用以下方式。

Directory.GetFiles(SourceDir,"*.*",SearchOption.AllDirectories)
于 2013-10-08T13:20:22.850 回答
0

试试这个,但首先你应该看看 files() 是否有完整的列表,然后再继续阅读文件。

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Dim SourceDir As String = Application.StartupPath & "\Source\"
    If Not Directory.Exists(SourceDir) Then
        Directory.CreateDirectory(SourceDir)
    End If
    Dim Files() As String = Directory.GetFiles(SourceDir)


    For Each File In Files
        Dim Lines() As String
        Dim StockList As List(Of ListViewItem) = New List(Of ListViewItem)
        Dim StockItem As ListViewItem
        Lines = IO.File.ReadAllText(File).Split(New String() {ControlChars.CrLf}, StringSplitOptions.RemoveEmptyEntries)
        StockItem = New ListViewItem(Lines(0))
        If Lines.Length > 1 Then
            For j = 1 To Lines.Length - 1
                StockItem.SubItems.Add(Lines(j))
            Next
        End If
        listHouse.Items.Add(StockItem)
    Next
End Sub
于 2013-10-08T14:49:19.230 回答