尝试获取所有可写目录vb.net时出现权限错误
我有一个函数,它假设返回给定目录中的所有可写路径尝试获取所有可写目录vb.net时出现权限错误,vb.net,permissions,directory,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()
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
Private函数getAllFolders(ByVal目录作为字符串)作为字符串()
Dim fi作为新IO.DirectoryInfo(目录)
Dim path()作为字符串={}
对于fi.GetDirectories()中作为IO.DirectoryInfo的每个子文件夹
将属性设置为System.IO.FileAttributes=My.Computer.FileSystem.GetFileInfo(subfolder.ToString).Attributes
如果(Attributes和IO.FileAttributes.ReadOnly)为0,则
数组.调整大小(路径,路径.长度+1)
路径(path.Length-1)=子文件夹.FullName
对于getAllFolders(subfolder.FullName)中的每个s作为字符串
Dim子属性为System.IO.FileAttributes=My.Computer.FileSystem.GetFileInfo(s.ToString.Attributes)
如果(子属性和IO.FileAttributes.ReadOnly)为0,则
数组.调整大小(路径,路径.长度+1)
路径(路径长度-1)=s
如果结束
下一个
如果结束
下一个
返回路径
端函数
似乎在处理权限问题时出现了一些错误,在尝试调试代码时,我一直得到“UnauthorizedAccessException was handled”,有什么建议吗?一些系统文件夹,如“文档和设置”或“系统卷信息”将生成该错误。您可以测试
fi
,以确保它不是for
循环之前的系统文件夹。如果有问题的目录具有导致问题的其他属性,则除了FileAttributes.System之外,还可以检查该属性
If (fi.Attributes And FileAttributes.System) = 0 Then
目录的值是多少?GetDirectories列出的某些目录向您的程序返回UnauthorizedAccessException是正常的。例如,尝试使用文件ExplorerIt打开C:\System卷信息它可以是任何文件夹或驱动器我尝试了每个驱动器并避免尝试System drive(C),如果您想知道我是从accessible directory@SteveError stopped rising启动的,但没有返回任何路径它似乎从系统文件夹启动for循环并停止,因此,仍然无法使用给定的驱动器号@xpda单步遍历循环并检查目录fi的名称。这将告诉您是哪个文件夹导致了问题。