我正在尝试在Win7 64位计算机上编写一个列出VB.net 2010 Express中自定义类的文件。我使用的帐户安装了VB.net express,该帐户对其所属的计算机和网络都具有管理员权限。我应该可以访问机器上的任何文件,也可以从Windows资源管理器访问。我被“System.UnauthorizedAccessException”阻止了。
我尝试过更改清单文件。虽然帐户已经具有完全权限,但不应该这样做。它没有奏效。以下是有问题的代码片段。
我开始怀疑是否可以使用托管代码读取驱动器上的文件。我可以恢复到几年前我写过的古代Win32 API调用的VBA类,但这违反了本练习的目的。任何建议将不胜感激。
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
… Loop through the file list and do something exciting
End If
Dim dirFolder() As DirectoryInfo = dirInfo.GetDirectories
If dirFolder.Length > 0 Then
… Loop through the folder list and do something exciting
sLoadCatalog(strfolder, intLastID)
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