循环文件时出现奇怪的行为

时间:2017-01-12 00:59:01

标签: vb.net visual-studio-2008 windows-7

这让我们发疯,这应该有效!

跑步时(步进)

它跳过debug.print(No Errors)

然后它命中i + = 1

永远不会在下一个(断点)停止

但是我= 51!

任何线索

If CheckBox8.Checked = False Then
        Exit Function
    Else
        Dim fInfo As FileInfo()
        Dim i As Integer = 0
        Dim dInfo As DirectoryInfo = New DirectoryInfo(spath.ToString)
        fInfo = dInfo.GetFiles("*.xml")
        Dim sfiles As String()
        Dim sFile As String
        sfiles = Directory.GetFiles(spath, "*.xml")
        For Each sFile In sfiles
            Try
                Debug.Print(sFile.ToString)
                i += 1
            Catch ex As Exception
                Debug.Print(ex.Message)
            End Try

        Next


    End If

1 个答案:

答案 0 :(得分:0)

此代码似乎可以正常工作

Imports System.IO

Module Module1

    Sub Main()
        Dim spath As String
        spath = "C:\YOUR_DIRECTORY"

        Dim fInfo As FileInfo()
        Dim i As Integer = 0
        Dim dInfo As DirectoryInfo = New DirectoryInfo(spath.ToString)
        Try
            fInfo = dInfo.GetFiles("*.xml")
            For Each fi In dInfo.GetFiles("*.xml")
                Dim file_name As String
                file_name = fi.Name
                Console.WriteLine(file_name)
                i = i + 1
            Next
            Console.WriteLine("Found:" + i.ToString + " Files")
        Catch ex As Exception
            Console.Write(ex.Message)
        End Try
    End Sub

End Module

注意:Debug.Print将打印到Visual Studio中的"输出窗口" ,而不是命令行,所以...... Debug.Print语句可能不会如果你没有打开输出窗口,那就完全可以了。

Degug.Print in VBA