我遇到异常问题:我使用的代码是:
Public Class Form1
Private Sub KiesPad_Click(sender As Object, e As EventArgs) Handles KiesPad.Click
Dim dlg As New FolderBrowserDialog
Dim result As DialogResult = dlg.ShowDialog()
If result = DialogResult.OK Then
For Each f As String In IO.Directory.EnumerateFileSystemEntries(dlg.SelectedPath, "*.*", IO.SearchOption.AllDirectories)
Try
Microsoft.VisualBasic.FileIO.FileSystem.RenameFile(f, FixName(f))
Me.RichTextBox1.Text = Me.RichTextBox1.Text & "Aangepast " & f & " in " & FixName(f) & vbCrLf
Catch ex As System.UnauthorizedAccessException
MsgBox("Security problem")
Catch ex As IO.IOException
'Bestandsnaam niet aangepast. Bestaat al, niks doen.
Catch ex As Exception
MsgBox(ex.Message & ex.HResult)
End Try
Next
End If
End Sub
运行此代码时,收到错误消息:
An unhandled exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll
Additional information: no access fot c:\users\jack\documents\pictures`
但我为System.UnauthorizedAccessException
我的错误是什么?
答案 0 :(得分:0)
调用IO.Directory.EnumerateFileSystemEntries
时,您不会捕获异常。将您的Try
区块向上移动:
Try
For Each f As String In IO.Directory.EnumerateFileSystemEntries(dlg.SelectedPath, "*.*", IO.SearchOption.AllDirectories)
Microsoft.VisualBasic.FileIO.FileSystem.RenameFile(f, FixName(f))
Me.RichTextBox1.Text = Me.RichTextBox1.Text & "Aangepast " & f & " in " & FixName(f) & vbCrLf
Next
Catch ex As System.UnauthorizedAccessException
MsgBox("Security problem")
Catch ex As IO.IOException
'Bestandsnaam niet aangepast. Bestaat al, niks doen.
Catch ex As Exception
MsgBox(ex.Message & ex.HResult)
End Try