尝试Catch ex作为UnauthorizedAccessException问题 [英] Try Catch ex As UnauthorizedAccessException Problem
问题描述
我正在使用树视图作为列出所有要扫描的目录,文件夹和子文件夹的方法.扫描开始正常,但出现异常拒绝访问文件夹系统卷信息".我知道足够使用try catch语句:
I am using a treeview as a means to list all directories, folders and subfolders to scan from. The scan starts off fine but then gives the exception "Access to folder System Volume Information is denied". I know enough to use a try catch statement:
(catch ex as unauthorizedAccessException)
但是扫描不会停止.....大声笑您可以说它运行良好,但不是我想要的.我不确定我是否将try catch语句放置在正确的位置,并希望有人可以简单地告诉我将其放置在我所处的位置.
这是包含例外的公共子目录:
but then the scan does not stop..... lol you could say it works to well but its not what im looking for. I am not sure if I am placing the try catch statement in the correct place and was hoping someone could simply tell me where to place it in my situation.
Here is the public sub that contains the exception:
Public Sub ScanScanner(ByVal dir As String)
Dim hs As String = "None"
With My.Computer.FileSystem
Application.DoEvents()
If stop_Scan = True Then
Exit Sub
End If
For Each file1 As String In .GetFiles(dir)
Application.DoEvents()
If stop_Scan = True Then
Button1.Enabled = True
Exit Sub
End If
txtScanningNow.Text = file1
If gt_extension_zip(IO.Path.GetExtension(file1)) = True Then
End If
Dim fi As FileInfo = New FileInfo(file1)
Call getmyfileatr(file1)
Dim fileDetail As IO.FileInfo
fileDetail = My.Computer.FileSystem.GetFileInfo(file1)
lblSizef.Text = fileDetail.Length &"byte(S)"
a1 = 0
a1 = (DateTime.Now.Millisecond)
a3 = 0
a3 = (DateTime.Now.Second)
Dim StartTime As New DateTime()
Dim EndTime As New DateTime()
StartTime = DateTime.Now()
f1 = 0
f2 = 0
a2 = 0
a2 = (DateTime.Now.Millisecond)
a4 = 0
a4 = (DateTime.Now.Second)
If a2 >= a1 Then
f1 = a2 - a1
End If
If a4 >= a1 Then
f2 = a4 - a3
End If
EndTime = DateTime.Now
Dim answer1, answer2 As Long
answer1 = 0
answer1 = EndTime.Ticks() - StartTime.Ticks()
txtTime.Text = String.Format("{0}.{1}.{2}", f2, f1, answer1)
txtTime.Refresh()
Next file1
'' Search child directories.
For Each folder As Object In .GetDirectories(dir)
Application.DoEvents()
txtScanningNow.Text = folder
ScanScanner(folder)
Next folder
End With
Exit Sub
End Sub
我尝试将try catch语句放在对于.files(dir)中的字符串中的每个file1"上,但是它使所有file1都在该错误下.
I have tried to place the try catch statement on "For Each file1 As String In .GetFiles(dir)" but it makes all file1 under that error.
推荐答案
Place它在每个线程的堆栈顶部.确保您永远不会阻止异常在中间的任何地方传播;如果捕获到任何异常,请始终将其重新引发在同一块中或引发另一个异常(除非以某种方式显示异常的最顶层,否则请不要将其完全隐藏).此规则很少有排除项,通常是万一某些设计不当的第三方库时不得已而为之.
对于System.Windows.Application
,"very top"位于主UI循环中.
这是这样的:将处理程序添加到Application.OnThreadException
(在UI中显示异常信息);要触发此事件,请将Application.SetUnhandledExceptionMode
分配给UnhandledExceptionMode.CatchException
.
注意!默认情况下不会发生!
哦,您正在使用DoEvents
!这几乎是绝对的邪恶,不要这样做,而应使用线程.这一直是许多问题和解答的主题.您可能想要搜索那些(我的个人资料有很好的建议)或尝试这样做并提出单独的问题.
(重要!请发表问题或评论,如果这不是答案,请不要执行添加答案",否则将被删除.)
—SA
Place it on the very top of the stack for each thread. Make sure you never block exception from propagation anywhere in the middle; if you catch any exception, always re-throw it in the same block or throw another exception (except the very top where you show exception somehow, never hide it completely). There are very few exclusions from this rule, usually as a last resort in case of some ill-designed 3-rd party libraries.
ForSystem.Windows.Application
, "very top" is in the main UI cycle.
This is how: add handler toApplication.OnThreadException
(show exception info in UI); to make this event firing, assignApplication.SetUnhandledExceptionMode
toUnhandledExceptionMode.CatchException
.
Attention! It won''t happen by default!
Oh, you''re usingDoEvents
! This is nearly the absolute evil, don'' do that, use threads instead. This has been a topic for many questions and answers here. You may want to search for those (my profile has good set of advices) or try to do it and ask separate question(s).
(Important! Please, post questions or comments, don''t do "Add Answer" if this is not an Answer — will be removed.)
—SA
这篇关于尝试Catch ex作为UnauthorizedAccessException问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!