如何传递拒绝访问的文件? [英] How to pass access denied files?
本文介绍了如何传递拒绝访问的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将C:\中的所有文件添加到目录和子目录中存在的列表框中,但是当检测到访问被拒绝的文件时,程序将失败.
到目前为止,我的代码是::)
I am trying to add all files from C:\ to the listbox that exist in directories and subdirectories but the program fails when an access denied file is detected.
the code I have so far is: :)
Imports System.IO
Public Class Form1
Inherits System.Windows.Forms.Form
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim startup_path As String = Application.StartupPath
txtDir.Text = startup_path.Substring(0, startup_path.LastIndexOf("\"))
txtDir.Select(0, 0)
End Sub
Private Sub btnList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnList.Click
Get the pattern without stuff in parens.
Dim pattern As String = cboPattern.Text
If pattern.IndexOf("(") >= 0 Then
pattern = pattern.Substring(0, pattern.IndexOf("("))
End If
lstFiles.Items.Clear()
Dim dir_info As New DirectoryInfo(txtDir.Text)
ListFiles(lstFiles, pattern, dir_info)
End Sub
'' Add the files in this directory''s subtree to the ListBox.
Private Sub ListFiles(ByVal lst As ListBox, ByVal pattern As String, ByVal dir_info As DirectoryInfo)
'' Get the files in this directory.
Dim fs_infos() As FileInfo = dir_info.GetFiles(pattern)
For Each fs_info As FileInfo In fs_infos
lstFiles.Items.Add(fs_info.FullName)
Next fs_info
fs_infos = Nothing
'' Search subdirectories.
Dim subdirs() As DirectoryInfo = dir_info.GetDirectories()
For Each subdir As DirectoryInfo In subdirs
ListFiles(lst, pattern, subdir)
Next subdir
End Sub
End Class
我在代码中的哪里可以添加一条try catch语句,或者可以使程序跳过访问被拒绝的文件并转到下一个文件的内容?
Where in the code can I add a try catch statement or something that will allow the program to skip the access denied file and go to the next?
推荐答案
我'' d逐步执行代码以确认要获取的确切异常类型,但是忽略不可访问文件的解决方案应类似于以下内容. (我没有对此进行测试,因此将其用作指南并仔细检查异常类型,等等.)
I''d step through the code to confirm what exact exception type you are getting, but the solution to ignore the inaccessible files should be something like the following. (I didn''t test this, so use it as a guide and double check the exception type, etc.)
' Get the files in this directory.
Dim fs_infos() As FileInfo
Try
fs_infos = dir_info.GetFiles(pattern)
For Each fs_info As FileInfo In fs_infos
... etc ...
Catch ex As AccessDeniedException
' No need to do anything, just ignore
End Catch
End Try
是的,例外是UnauthorizedAccessException
,这就是为什么我建议您对其进行测试的原因.还有2个可能发生异常的地方,因此您需要分别捕获它们.尝试这个;它适用于我的系统:
Yeah, the exception isUnauthorizedAccessException
, which is why I recommended that you should test it. There are also 2 places where the exception can happen, so you need to trap them both separately. Try this; it works on my system:
Private Sub ListFiles(ByVal lst As ListBox,
ByVal pattern As String, ByVal dir_info As DirectoryInfo)
'' Get the files in this directory.
Dim fs_infos() As FileInfo
Try
fs_infos = dir_info.GetFiles(pattern)
For Each fs_info As FileInfo In fs_infos
lst.Items.Add(fs_info.FullName)
Next fs_info
Catch ex As UnauthorizedAccessException
End Try
fs_infos = Nothing
'' Search subdirectories.
Dim subdirs() As DirectoryInfo
subdirs = dir_info.GetDirectories()
Try
For Each subdir As DirectoryInfo In subdirs
ListFiles(lst, pattern, subdir)
Next subdir
Catch ex As UnauthorizedAccessException
End Try
End Sub
这篇关于如何传递拒绝访问的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文