未经授权的访问异常 [英] Unauthorized Access Exception

查看:71
本文介绍了未经授权的访问异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试通过文件系统递送时发现的每个文件搜索器示例应用程序都会在1个文件夹或另一个文件夹上遇到UnauthorizedAccessException。

我一直在网上搜索一段时间但没有出现有一个可用的解决方案,因为大多数安全示例都适用于控制台应用程序,并且不适合Windows窗体应用程序或简单无法工作。



我正在尝试1调用使用后台工作程序的文件系统示例的VS2008示例。



示例下载:

downloads\vs 2008 rtm vbsamples \ vb 2008 rtm samples\language samples\filesystem\FileSystemSample.sln



此示例中没有任何导入。



我想要做的是能够在文件中搜索字符串并返回文件列表,示例应用程序会执行该操作直到达到异常。



到目前为止,我一直在使用wmi类进行搜索文件,但不是内容,以解决异常问题,但需要为此特定目的。



任何帮助表示赞赏

解决方案

驱动器上有文件夹和文件,没有帐户有权进入。您必须编写代码来处理这种情况,例如在Try / Catch块中包装GetFiles和GetDirectories调用。这真的不是那么难。


我认为这就是你想要的:



使用这段代码,我得到了我可以访问的c:驱动器上的每个文件的列表,不包括我不访问的文件。没有抛出异常(无论如何都没有被抓住)。当你有列表时,它很简单,可以枚举它们来搜索你的字符串。



 私人 功能 GetFilesList( ByVal  theFolder  As   String 可选  ByVal  recurseSubdirectories  As  < span class =code-keyword> Boolean  =  True  As  List(  字符串
Dim filesList < span class =code-keyword>作为 列表( 字符串
Dim foldersList 作为 列表( 字符串
Dim tmp 作为 列表( 字符串

Dim dir As IO.DirectoryInfo(theFolder)
如果 dir.Exists 然后
' 我们的文件夹不存在。请在此处理。
返回
结束 如果

' < span class =code-comment>获取此文件夹中的文件...
尝试
filesList.AddRange(目录.GetFiles(theFolder))
foldersList.AddRange(Directory.GetDirectories(theFolder))

Catch ex As 异常
' 该文件夹存在,但我们没有访问它。
返回 没什么
结束 尝试

' 他们只想要这个文件夹中的文件吗?
如果 recurseSubdirectories 然后
返回 filesList
其他
' 如果此文件夹中有目录处理它们
对于 每个文件夹作为 字符串 foldersList
tmp = GetFilesList(文件夹)
如果 tmp IsNot 没什么 然后 filesList.AddRange(tmp)
下一步

返回 filesList
结束 如果

结束 功能


它有一个try catch块,

这是后台工作者



 私人  Sub  BackgroundWorker1_DoWork( ByVal  sender 作为 对象 ByVal  e  As  System.ComponentModel.DoWorkEventArgs) Handles  BackgroundWorker1.DoWork 
尝试
Dim 个文件为 System.Collections.ObjectModel.ReadOnlyCollection( Of String
' 查看与提供的通配符匹配的每个文件
If (递归)然后
files = My.Computer.FileSystem.FindInFiles(目录,searchText, True ,FileIO.SearchOption.SearchAllSubDirectories,wildCards)
Else
files = My.Computer.FileSystem.FindInFiles(directory,searchText, True ,FileIO.SearchOption.SearchTopLevelOnly,wildCards)
结束 如果
对于 每个 foundFile 作为 字符串 个文件中
.BackgroundWorker1.ReportProgress( 0 ,foundFile)
下一页
Catch ex As 例外
MsgBox(ex.Message& vbCrLf& ex.Source,MsgBoxStyle.Information, 异常消息
结束 尝试
结束


Every File searcher sample app I find when trying to recurse thru the file system runs into a UnauthorizedAccessException on 1 folder or another.
I have been searching the web for a while and have not come up with a usable solution as most examples for security are for console apps and don't lend well to windows form apps or plain don't work.

I am trying 1 of the VS2008 samples thats called filesystemsample that uses the background worker.

The sample was a download:
downloads\vs 2008 rtm vbsamples\vb 2008 rtm samples\language samples\filesystem\FileSystemSample.sln

There are no imports for anything in this sample.

What I am Wanting to do it to be able to search for a string in a file and return a list of files , which the sample app does till it hits the exception.

So far I have been using a wmi class for searching for files but not contents to get around the exception problem but need something for this particular purpose.

Any help is appreciated

解决方案

There are folders and files on the drive that no account has permissions to get into. You're code has to be written to deal with this situation, like wrapping GetFiles and GetDirectories calls in a Try/Catch block. It's really not that difficult.


I think this is what you wanted:

Using this code, I was able to get a list of every file on my c: drive that I have access to, excluding those I don't. No exception was thrown (that wasn't caught, anyway). When you have the list it's simple enough to enumerate them to search for your string.

Private Function GetFilesList(ByVal theFolder As String, Optional ByVal recurseSubdirectories As Boolean = True) As List(Of String)
        Dim filesList As New List(Of String)
        Dim foldersList As New List(Of String)
        Dim tmp As New List(Of String)

        Dim dir As New IO.DirectoryInfo(theFolder)
        If Not dir.Exists Then
            ' Our folder doesn't exist. Handle it here.
            Return Nothing
        End If

        ' Get the files in this folder...
        Try
            filesList.AddRange(Directory.GetFiles(theFolder))
            foldersList.AddRange(Directory.GetDirectories(theFolder))

        Catch ex As Exception
            ' The folder exists, but we don't have access to it.
            Return Nothing
        End Try

        ' Do they only want the files in THIS folder?
        If Not recurseSubdirectories Then
            Return filesList
        Else
            'If there are directories in this folder process them
            For Each folder As String In foldersList
                tmp = GetFilesList(folder)
                If tmp IsNot Nothing Then filesList.AddRange(tmp)
            Next

            Return filesList
        End If

    End Function


It has a try catch block,
Here is the background worker

Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
       Try
           Dim files As System.Collections.ObjectModel.ReadOnlyCollection(Of String)
           ' Look in each file matching the provided wildcards
           If (recurse) Then
               files = My.Computer.FileSystem.FindInFiles(directory, searchText, True, FileIO.SearchOption.SearchAllSubDirectories, wildCards)
           Else
               files = My.Computer.FileSystem.FindInFiles(directory, searchText, True, FileIO.SearchOption.SearchTopLevelOnly, wildCards)
           End If
           For Each foundFile As String In files
               Me.BackgroundWorker1.ReportProgress(0, foundFile)
           Next
       Catch ex As Exception
           MsgBox(ex.Message & vbCrLf & ex.Source, MsgBoxStyle.Information, "Exception Message")
       End Try
   End Sub


这篇关于未经授权的访问异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆