避免在出错时退出for / next

时间:2014-12-03 17:30:35

标签: vb.net visual-studio-2010

我应该知道在寻求帮助时不要提供足够的信息/好的信息。我还有一个问题需要删除/链接或合并到这里...任何关于如何做的评论都会很棒。

所以问题是我正在尝试编写一个简单的应用程序(是的,右)在本地硬盘上找到一个特定的文件。将有多个文件实例(备份等),我想找到每个。问题是我不知道有多少本地硬盘,我不知道文件可能在哪个目录。

所以我想

  1. 遍历所有固定硬盘
  2. 在hdd中搜索文件(我总是知道文件的名称)
  3. 我像这样搜索硬盘......

    For Each drive As IO.DriveInfo In IO.DriveInfo.GetDrives
    
    If drive.DriveType.ToString = "Fixed" Then
    

    因此,如果找到的硬盘已修复,我会这样做......

     Try
    
     For Each filename As String In IO.Directory.GetFiles(hdd, "user.config", IO.SearchOption.AllDirectories)
    

    问题是我抛出一个异常,我在try catch中使用了for block,异常被捕获并且代码退出。

    好的,所以我想这是可以预料的。有关如何避免这种情况的任何建议?意思是我知道我会遇到我没有权限的文件/目录,如何继续并忽略我不关心的内容?

    完整的代码列表

     Private Sub Command1_Click(sender As System.Object, e As System.EventArgs) Handles Command1.Click
        'variable to hold each hdd name eg: c:\
        Dim hdd As String
        Dim directories As Integer = 0
        Dim files As Integer = 0
    
        Try
    
            For Each drive As IO.DriveInfo In IO.DriveInfo.GetDrives
    
                If drive.DriveType.ToString = "Fixed" Then
    
                    'asign the variable hdd a name for this run through
                    hdd = drive.Name
    
                    'assign label value for directories
                    directories = directories + 1
                    Label6.Text = directories.ToString
                    Me.Refresh()
    
    
                    'search the hdd for the file user.config
                    Try
    
    
    
                        For Each filename As String In IO.Directory.GetFiles(hdd, "user.config", IO.SearchOption.AllDirectories)
                            Me.Refresh()
    
                            '    'assign label value for files
                            files = files + 1
                            Label2.Text = files.ToString
    
                            Me.Refresh()
                            '    'variable to hold the path for each found file
                            Dim file As String = IO.Path.GetFullPath(filename)
    
                            '    'update file checking label
                            Label4.Text = file.ToString
    
                            '    'add each found file to the list
                            List1.Items.Add(file)
    
                        Next
                    Catch ex As Exception
                    End Try
    
                End If
    
    
    
            Next
    
        Catch ex As Exception
            MsgBox(ex.ToString)
    
        End Try
        'iterate through each found drive
    
    
    
    
    
    End Sub
    

1 个答案:

答案 0 :(得分:0)

您需要长时间写出来,因为您需要处理IO安全问题。当然还有一个递归函数。

被叫:

FindFileOnAllDrivesOfType(System.IO.DriveType.Fixed, "test.txt")

方法:

Public Function FindFileOnAllDrivesOfType(ByVal DriveType As System.IO.DriveType, ByVal FileToFind As String) As List(Of String)
    Dim AllLocations As New List(Of String)

    For Each drive As IO.DriveInfo In IO.DriveInfo.GetDrives
        If drive.DriveType = DriveType Then
            AllLocations = FindFile(drive.ToString(), FileToFind)
        End If
    Next

    Return AllLocations
End Function

Public Function FindFile(ByVal StartDirectory As String, ByVal FileToFind As String) As List(Of String)
    Dim AllLocations As New List(Of String)

    Try
        For Each dirname As String In IO.Directory.GetDirectories(StartDirectory)
            AllLocations.AddRange(FindFile(dirname, FileToFind))
        Next

        For Each filename As String In IO.Directory.GetFiles(StartDirectory, FileToFind, IO.SearchOption.TopDirectoryOnly)
            AllLocations.Add(filename)
        Next
    Catch ex As Exception
        ' MessageBox.Show(ex.Message)
        'Do nothing as we can't get a directory listing
    End Try

    Return AllLocations
End Function