使用vb.net进行递归搜索代码优化

时间:2015-02-18 10:01:36

标签: vb.net optimization

我创建了一个小程序来搜索包含扫描文档的一组文件夹。

文件夹结构如下:

C:\ IMAGES \年\月\日期\ documenttype \ firstpartofdocumentNo \

年份文件夹包含2005年至2015年的年份

月份文件夹包含一年中的几个月(显然​​) 与日期相同

documenttype文件夹可以包含1到5个文件夹

firstpartofdocumentno。可以包含1到3个文件夹

我目前使用的代码是:

CompName = Environment.MachineName

    'MsgBox(CompName)

    TicketNo = TxtTicketNo.Text

    If CompName = "Comp1" Then
        ImageDir = "C:\Images\"
    Else
        ImageDir = "\\Comp1\Images\"
    End If

    For Each DirYear As String In Directory.GetDirectories(ImageDir)
        Dim YearInfo As New DirectoryInfo(DirYear)

        For Each DirMonth As String In Directory.GetDirectories(DirYear)
            Dim MonthInfo As New DirectoryInfo(DirMonth)

            For Each DirDate As String In Directory.GetDirectories(DirMonth)
                Dim DateInfo As New DirectoryInfo(DirDate)

                For Each DirType As String In Directory.GetDirectories(DirDate)
                    Dim TypeInfo As New DirectoryInfo(DirType)

                    For Each DirStart As String In Directory.GetDirectories(DirType)
                        Dim StartInfo As New DirectoryInfo(DirStart)

                        MainDirectory = ImageDir & YearInfo.Name & "\" & MonthInfo.Name & "\" & DateInfo.Name & "\" & TypeInfo.Name & "\" & StartInfo.Name & "\"
                        'LstFiles.Items.Add(YearInfo.Name & "\" & MonthInfo.Name & "\" & DateInfo.Name & "\" & TypeInfo.Name & "\")
                        'Dim files() As String = Directory.GetFiles(MainDirectory, TicketNo & "*")
                        'For Each Ticket As String In Directory.GetFiles(MainDirectory, TicketNo)
                        For Each Ticket As String In Directory.GetFiles(MainDirectory, TicketNo & "*")
                            LstFiles.Items.Add(Ticket)
                        Next

                        'Next


                    Next
                    'MsgBox(files)


                    'LstFiles.Items.Add()
                Next
                'LstFiles.Items.Add(dirInfo.Name)
            Next
        Next
        'MsgBox(ImageDir)
    Next

我在表单上有一个文本框,用于输入ticketno的最后四个数字,然后在单击该按钮时运行此代码。

问题是它可能需要长达五分钟的时间才能进行搜索,所以我想知道是否有一种方法可以优化此代码以加快速度,或者这听起来是否适合搜索那么多文件夹。

提前致谢   加雷

0 个答案:

没有答案