条件,文件存在,示例代码,检查文件目录

时间:2015-08-24 15:21:33

标签: .net vb.net file getfiles

大家好,我只是.net编程的初学者,我真的需要一些关于这个问题的帮助,我的代码的逻辑“应该”看看放在数组内的每个目录..然后将验证如果每个文件夹里面都有文件,那就是我使用“system.io.file.exist()”的原因,对不起代码请帮帮我...

        Dim schar As String
        Dim number As Integer
        Dim Dirs(4) As String
        Dirs(0) = "E:\_net_programs\test\sample1"
        Dirs(1) = "E:\_net_programs\test\sample2"
        Dirs(2) = "E:\_net_programs\test\sample3"
        Dirs(3) = "E:\_net_programs\test\sample4"
        Dirs(4) = "E:\_net_programs\test\sample5"
        For Each folder As String In Dirs

            Dim getDir As New DirectoryInfo(folder)
            Dim fiArr As FileInfo() = getDir.GetFiles()
            Dim fri As FileInfo
            For Each fri In fiArr

                If Not System.IO.File.Exists(folder + "\" + fri.Name) Then
                    MessageBox.Show("file does not exist. : " + folder + "\" + fri.Name)
                    Return
                Else
                    MessageBox.Show(folder + "\" + fri.Name)
                    Return
                End If

                Label1.Text = "textfile name :" & fri.Name
                Dim file As New System.IO.StreamReader(folder + "\" + fri.Name)
                schar = Trim(file.ReadLine())
                MessageBox.Show(schar)

                Do While file.Peek <> -1
                    schar = Trim(file.ReadLine())

1 个答案:

答案 0 :(得分:0)

如果您只对文本(* .txt)文件感兴趣,那么您应该这样做:

    Dim Dirs(4) As String
    Dirs(0) = "E:\_net_programs\test\sample1"
    Dirs(1) = "E:\_net_programs\test\sample2"
    Dirs(2) = "E:\_net_programs\test\sample3"
    Dirs(3) = "E:\_net_programs\test\sample4"
    Dirs(4) = "E:\_net_programs\test\sample5"
    For Each folder As String In Dirs
        Dim files = IO.Directory.GetFiles(folder, "*.txt")
        If files.Count = 0 Then
            MessageBox.Show("No txt files in folder : " + folder)
            Return
        Else
            MessageBox.Show(Join(files, vbCrLf))
            Return
        End If

    Next