在vb中从dir读取每个文件时出错

时间:2017-01-27 12:57:15

标签: vb.net

我正在尝试逐个读取目录中的所有文件,并且还阅读内部文本时出现此错误

An unhandled exception of type 'System.NullReferenceException' occurred in WindowsApplication1.exe

Additional information: Object reference not set to an instance of an object.

它仅适用于第一个文件,但是当它启动另一个循环并从该文件夹获取secound文件时出现了问题。 这是我的代码

Dim theString As String
        Dim path As String
        Dim StrFile, str_5 As String
        Dim fso As New FileSystemObject
        Dim file As TextStream
        Dim line As String

        'theString = ""
        path = "C:\IN\"

        StrFile = Dir(path & "*.txt")

        Do While StrFile <> ""

            file = fso.OpenTextFile(path & StrFile)
            Do While Not file.AtEndOfLine
                line = file.ReadLine
                If InStr(1, line, line.Substring(142, 1), vbTextCompare) = 5 Then
                    'Debug.Print(StrFile)
                    str_5 = str_5 & line

                End If
            Loop

            file.Close()
            file = Nothing
            fso = Nothing

            StrFile = Dir()
        Loop

1 个答案:

答案 0 :(得分:0)

对于有同样问题的其他人,答案就是删除这一行 fso =没什么