89tqYvEj7b
New member
- Joined
- Mar 4, 2010
- Messages
- 3
- Programming Experience
- 10+
I am writing a class to list the files in a folder or drive. I am using vb.net express 2010 installed on a Win7 64 bit machine. VB.net was installed using an account with admin rights to the machine and the network on which it resides. I have been stopped cold by a System.UnauthorizedAccessException. I have tried changing the manifest, as recommended elsewhere. The exception remains. I can read any file in Windows Explorer. I can use a VBA class using Win32 API calls. But I cannot seem to overcome the exception using managed code. Below is a code fragment that is producing the error. Any suggestions would be helpful.
Private Sub sLoadCatalog(ByVal strPath As String, ByVal intParentID As Integer)
'Get the file list
Dim strsearchPattern As String = "*"
Dim fp As New FileIOPermission(FileIOPermissionAccess.PathDiscovery Or FileIOPermissionAccess.Read, strPath)
Try
'fp.Demand()
fp.Assert()
Dim dirInfo As DirectoryInfo = New DirectoryInfo(strPath)
Dim FileList() As FileInfo = dirInfo.GetFiles()
If FileList.Length > 0 Then
Dim intLow As Integer = FileList.GetLowerBound(0)
Dim intHigh As Integer = FileList.GetUpperBound(0)
For intCur As Integer = intLow To intHigh
Dim strfile As String = FileList(intCur).FullName
Dim clOneFile As New ClassFileItem
With clOneFile
intLastID += 1
.Item_ID = intLastID
.ItemName = FileList(intCur).Name
.ItemType = "File"
.lastAccessed = FileList(intCur).LastAccessTime
.Parent_ID = intParentID
.ParentFolderPath = FileList(intCur).DirectoryName
.SizeInBytes = FileList(intCur).Length
.FullPath = FileList(intCur).FullName
.DateCreated = FileList(intCur).CreationTime
.DateModified = FileList(intCur).LastWriteTime
.Extension = FileList(intCur).Extension
End With
Dim strKey As String = "N" & CStr(intLastID)
colFileList.Add(clOneFile, strKey)
Next
'Else
'Exit Sub
End If
Dim dirFolder() As DirectoryInfo = dirInfo.GetDirectories
If dirFolder.Length > 0 Then
'recursive call
Dim intLow As Integer = dirFolder.GetLowerBound(0)
Dim intHigh As Integer = dirFolder.GetUpperBound(0)
For intCur As Integer = intLow To intHigh
Dim strfolder As String = dirFolder(intCur).FullName
Dim clNewFolder As New ClassFileItem
With clNewFolder
intLastID += 1
.Item_ID = intLastID
.ItemName = dirFolder(intCur).Name
.DateCreated = dirFolder(intCur).CreationTime
.DateModified = dirFolder(intCur).LastWriteTime
.lastAccessed = dirFolder(intCur).LastAccessTime
.FullPath = dirFolder(intCur).FullName
.ItemType = "Folder"
.Parent_ID = intParentID
.ParentFolderPath = dirFolder(intCur).FullName
End With
Dim strKey As String = "N" & CStr(intLastID)
colFileList.Add(clNewFolder, strKey)
sLoadCatalog(strfolder, intLastID)
Next
Else
Exit Sub
End If
Catch e As SecurityException
'MessageBox.Show("Security Cannot access folder " & strPath)
Catch k As UnauthorizedAccessException
'MessageBox.Show("Unauthorized Cannot access folder " & strPath)
End Try
End Sub
Last edited by a moderator: