Catch Catch ex As System.UnauthorizedAccessException

murrayf1

Member
Joined
May 5, 2006
Messages
21
Programming Experience
1-3
Hi im trying to catch an error
Catch ex As System.UnauthorizedAccessException but i cant seem to get it to work how do i do this?

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

For Each filePath As String In IO.Directory.GetFiles("C:\", "*.ptf", IO.SearchOption.AllDirectories)
IO.File.Copy(filePath, "F:\" & IO.Path.GetFileName(filePath), True)

Try

Catch ex As System.UnauthorizedAccessException

End Try

Return
My.Application.Log.WriteEntry("An Error Occurred: " & e.ToString())




Next filePath
End Sub
 
Hi,
The statement where you assumed can be error should be within the TRY-CATCH block.

Your code should be like this:
VB.NET:
[SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#800000]filePath[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#800000]IO[/COLOR][/SIZE][SIZE=2].[/SIZE][SIZE=2][COLOR=#800000]Directory[/COLOR][/SIZE][SIZE=2].[/SIZE][SIZE=2][COLOR=#800000]GetFiles[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#ff00ff]"C:\"[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#ff00ff]"*.ptf"[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#800000]IO[/COLOR][/SIZE][SIZE=2].[/SIZE][SIZE=2][COLOR=#800000]SearchOption[/COLOR][/SIZE][SIZE=2].[/SIZE][SIZE=2][COLOR=#800000]AllDirectories[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#800000]IO[/COLOR][/SIZE][SIZE=2].[/SIZE][SIZE=2][COLOR=#800000]File[/COLOR][/SIZE][SIZE=2].[/SIZE][SIZE=2][COLOR=#800000]Copy[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#800000]filePath[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#ff00ff]"F:\"[/COLOR][/SIZE][SIZE=2] & [/SIZE][SIZE=2][COLOR=#800000]IO[/COLOR][/SIZE][SIZE=2].[/SIZE][SIZE=2][COLOR=#800000]Path[/COLOR][/SIZE][SIZE=2].[/SIZE][SIZE=2][COLOR=#800000]GetFileName[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#800000]filePath[/COLOR][/SIZE][SIZE=2]), [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#800000]ex[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#800000]System[/COLOR][/SIZE][SIZE=2].[/SIZE][SIZE=2][COLOR=#800000]UnauthorizedAccessException[/COLOR][/SIZE][SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][CODE]
 
 
Thanks,
Jay
 
IO.Directory.GetFiles may throw UnauthorizedAccessException too.
 
Yip thats throwing an error aswell,

How can i stop that i have tried putting some try and catchs around it but its not happy.

Any ideas would be good.
 
in the try catch block provide an extra catch statement incase the exeption you are trying to a catch is not the fail. For example:

VB.NET:
[SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][SIZE=2]    'all code [COLOR=blue]here[/COLOR]
[COLOR=#0000ff]Catch [COLOR=black]ex [/COLOR]As [/COLOR][COLOR=black]System.UnauthorizedAccessException[/COLOR]
     'log the exception
[COLOR=blue]Ca[/COLOR][/SIZE][SIZE=2][COLOR=blue]tch[/COLOR][/SIZE][SIZE=2] ex [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Exception
     'log this exception[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE]

In the above, if the exception your asking for first is thrown it will catch it, if not the try catch will not catch it. So provide a default exception to catch other potential errors.
 
VB.NET:
        Try
            For Each filePath As String In IO.Directory.GetFiles("C:\", "*.ptf", IO.SearchOption.AllDirectories)

                IO.File.Copy(filePath, "F:\" & IO.Path.GetFileName(filePath), True)
            Catch ex As System.UnauthorizedAccessException
        Catch ex As Exception


        End Try

Ok where do i put the next in the above code ?
 
no don't do it that way, it will crash the entire loop. Put the entire try catch statement inside the loop.


First here declare an array with the directory.getfiles

Try
Myarray = directory.getfiles
Catch
MyArray = nothing
End Try

If MyArray isnot nothing then
For....

Try
...copy the file ect....
Catch
End Try

Next

end if



This way it cancels if the directory can't be reached and also cancels if the file can't be copied, but continues on.
 
You want to Try something for every iteration of the loop, so you put the Try block inside the loop. That way, if that iteration fails the loop will continue. This is really only repeating what has already been posted but:
VB.NET:
For Each filePath As String In Whatever
    Try
        'Try to open the current file.
    Catch
        'Catch en exception here.
    End Try

    'Now the loop will continue whether an error occurred or not.
Next
 
VB.NET:
            For Each filePath As String In IO.Directory.GetFiles("C:\", "*.ptf", IO.SearchOption.AllDirectories)
Thats the part thats causing the error, that is outside the try catch part, if i put it inside the try catch bit it doesnt work anymore
It is crashing on system volume information
 
Ok, here is the exact code to search the folder and all it's sub folders. Get what you can out of this and ask me questions bout it if need be. Modify it as you like. I use this code to search for mp3 files but I cut the parts that restrict it to .mp3 and add it to my array. I left only the important parts for you to plunder with. I wrote this routine myself, so there may be an easier more effiecient way, but this works great. Also notice the try catch statements are used exactly like you want. I have more code that looks over system volume directories and all but I specifically tell it to.

Please let me know if this helps. You code should call the SearchFolder sub first with the directory that you are searching. So really the subs are listed backwards, try not to get confused I aint got time to switch them. . BEdtime.

VB.NET:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] FindFiles([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] Folder [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]               ' ^here i replaced FindSongs to FindFiles to fix the problem...
[/SIZE][SIZE=2][COLOR=#0000ff]    If[/COLOR][/SIZE][SIZE=2] Folder [/SIZE][SIZE=2][COLOR=#0000ff]Is[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Nothing[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Or[/COLOR][/SIZE][SIZE=2] Folder = [/SIZE][SIZE=2][COLOR=#800000]""[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Return
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]         Try
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]              For[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] f [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] Directory.GetFiles(Folder)
[/SIZE][SIZE=2][COLOR=#0000ff]                   If[/COLOR][/SIZE][SIZE=2] f [/SIZE][SIZE=2][COLOR=#0000ff]Is[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Nothing[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Or[/COLOR][/SIZE][SIZE=2] f = [/SIZE][SIZE=2][COLOR=#800000]""[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Return
[/COLOR][/SIZE][SIZE=2]                  '.... your code here for copying the file[/SIZE][SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff]              Next
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]         Catch[/COLOR][/SIZE][SIZE=2] ex [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Exception
 
[/SIZE][SIZE=2][COLOR=#0000ff]         End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
 
[SIZE=2][COLOR=#0000ff][COLOR=black]'call this sub first [/COLOR]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] SearchFolder([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] Folder [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2]) 'this routine is recursive
     FindFiles(Folder)
[/SIZE][SIZE=2][COLOR=#0000ff]    Dim[/COLOR][/SIZE][SIZE=2] Files() [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#0000ff]Nothing
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]    Try
[/COLOR][/SIZE][SIZE=2]         Files = Directory.GetDirectories(Folder)
[/SIZE][SIZE=2][COLOR=#0000ff]    Catch[/COLOR][/SIZE][SIZE=2] ex [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Exception
[/SIZE][SIZE=2][COLOR=#0000ff]        Return
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]    End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]    If[/COLOR][/SIZE][SIZE=2] Files [/SIZE][SIZE=2][COLOR=#0000ff]Is[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Nothing[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Return
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]    For[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] F [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] Files
          SearchFolder(F)
[/SIZE][SIZE=2][COLOR=#0000ff]    Next
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]
 
Last edited:
Sorry, replace the above sub FindSongs to FindFiles, my bad.


I edited the code so you can see what I changed just look up.
 
who hoo it works,

just one little question how do i limit it to one file type?

im using but before i limited it in the finding side of things but i cant see how to do this
IO.File.Copy(filePath, "F:\" & IO.Path.GetFileName(filePath), True)
 
Back
Top