之前寫程式時候寫的一個小模組,雖然 VB.Net 有內建函數可以使用,但是取得的是完整的路徑,會導致一些麻煩,就寫了一個模組,可以指定目錄和要篩選的檔案類型,不過還有很多可以改進的空間就是了,程式碼如下:

Imports System.IO

Module ModuleFileList

    Public Function ListFile(ByVal dPath As String, ByRef FileList() As String, ByVal SearchPattern As String) As Boolean
        Try
            If Directory.Exists(dPath) Then
                Dim D As Directory
                Dim i, j, k As Integer
                Dim Temp() As String = D.GetFiles(dPath)
                Array.Sort(Temp)
                Dim sPattern() = SearchPattern.Split("|")

                ReDim FileList(UBound(Temp))
                k = 0

                For i = 0 To UBound(sPattern)
                    For j = 0 To UBound(Temp)
                        If Path.GetExtension(Temp(j)) = sPattern(i) Then
                            FileList(k) = Strings.Right(Temp(j), Temp(j).Length - Strings.InStrRev(Temp(j), ""))
                            k += 1
                        End If
                    Next
                Next
                ReDim Preserve FileList(k - 1)
                Return True
            End If
        Catch ex As Exception
            ReDim FileList(0)
            MessageBox.Show("找不到指定的資料夾", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return True
        End Try
    End Function

End Module
2008/09/17

本站採用CC授權不歡迎全文轉載,如需轉載(部份)、引用文章,請務必附上作者及來源處。

0 留言

發表留言 »





訂閱網站

Copyright 2008 黯雲資訊小站 | Theme Sco v4.0 By:Scorpio