VB.NET: GetFiles Method - "Access to the path 'G:\System Volume Information' is denied."
Here is my code,
Dim allFiles As FileInfo() =
tempDir.GetFiles("*.pdf", SearchOption.AllDirectories)
I've googled and found that I need to change the permissions of my app from
Project properties > View UAC Settings > and change level to level="requireAdministrator"
But i开发者_Go百科ts also not working. I found something about FileIOPermission
class, but dont know how to implement it.
==> Detailed code.
Dim tempDir As New DirectoryInfo(path)
Dim FileDetails(4) As String
Dim iTem As ListViewItem
If (tempDir.Attributes <> FileAttributes.System) Then
Dim allFiles As FileInfo() = tempDir.GetFiles("*.pdf", SearchOption.AllDirectories)
Dim oneFIle As FileInfo
For Each oneFIle In allFiles
FileDetails(0) = oneFIle.Name()
FileDetails(1) = oneFIle.FullName()
FileDetails(2) = oneFIle.Length() / (1024 * 1024)
FileDetails(2) = FileDetails(2).Remove(5)
iTem = New ListViewItem(FileDetails)
ListView1.Items.Add(iTem)
Next
End If
Path is a string that contains the path required, in this case G:\
You won't find PDF files in this folder:
The
System Volume Information
folder is a hidden system folder that the System Restore tool uses to store its information and restore points. (MSDN)
So just ignore it.
Granted, GetFiles()
does not allow you to ignore files/folders, so you'd have to PInvoke into FindFirstFile et al. to do searches effectively.
Ok, I think I solved the case, I just iterated each folder, checked their attributes and then added to the list.. I think it's working.. Plz check it a bit..
Dim tempDir As New DirectoryInfo(path)
Dim FileDetails(4) As String
Dim iTem As ListViewItem
Try
Dim allFiles As FileInfo() = Nothing
For Each Directory In tempDir.GetDirectories()
Try
If (Directory.Attributes <> FileAttributes.System) Then
allFiles = Directory.GetFiles("*.pdf", SearchOption.AllDirectories)
End If
Dim oneFIle As FileInfo
For Each oneFIle In allFiles
FileDetails(0) = oneFIle.Name()
FileDetails(1) = oneFIle.FullName()
FileDetails(2) = oneFIle.Length() / (1024 * 1024)
FileDetails(2) = FileDetails(2).Remove(5)
iTem = New ListViewItem(FileDetails)
ListView1.Items.Add(iTem)
Next
Catch ex As Exception
End Try
Next
Catch ex As UnauthorizedAccessException
End Try
System Volume Information Folder is a O/S protected folder. Even though you may have administrative access, you still will not be able to access it. You can try it from Explorer itself. (Need to enable option to show protected Operating System files.)
精彩评论