尝试获取所有可写目录vb.net时出现权限错误

时间:2014-11-28 23:46:52

标签: vb.net permissions directory

我有这个函数,它假设返回给定目录中的所有可写路径

    Private Function getAllFolders(ByVal directory As String) As String()

    Dim fi As New IO.DirectoryInfo(directory)
    Dim path() As String = {}
    For Each subfolder As IO.DirectoryInfo In fi.GetDirectories()
        Dim Attributes As System.IO.FileAttributes = My.Computer.FileSystem.GetFileInfo(subfolder.ToString).Attributes
        If (Attributes And IO.FileAttributes.ReadOnly) <> 0 Then
            Array.Resize(path, path.Length + 1)
            path(path.Length - 1) = subfolder.FullName
            For Each s As String In getAllFolders(subfolder.FullName)
                Dim SubAttributes As System.IO.FileAttributes = My.Computer.FileSystem.GetFileInfo(s.ToString).Attributes
                If (SubAttributes And IO.FileAttributes.ReadOnly) <> 0 Then
                    Array.Resize(path, path.Length + 1)
                    path(path.Length - 1) = s
                End If
            Next
        End If
    Next
    Return path
End Function

似乎处理权限问题有些错误,我一直在处理&#34; UnauthorizedAccessException已被处理&#34;在尝试调试代码时,有什么建议吗?

1 个答案:

答案 0 :(得分:1)

某些系统文件夹,例如&#34;文档和设置&#34;或&#34;系统卷信息&#34;会产生那个错误。您可以测试fi以确保它在for循环之前不是系统文件夹。如果违规目录有一些导致问题的其他属性,除了FileAttributes.System之外,您还可以检查该属性。

If (fi.Attributes And FileAttributes.System) = 0 Then