|
-
Mar 7th, 2009, 01:48 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] [2005] Error catching
I'm trying to improve my app with some error catching.
I have a module that downloads a file from the Internet to a local folder. At the moment, if the local folder doesn't exist, it just crashes here:
Code:
strLocalFile = New FileStream(pPath, FileMode.Create, FileAccess.Write, FileShare.None)
with error:
System.IO.DirectoryNotFoundException was unhandled
Message="Could not find a part of the path 'C:\Documents and Settings\Simon\Desktop\test\download.zip'."
Source="mscorlib"
How can I do something with that error and stop my app from crashing?
Ideally, have a messagebox show saying 'directory not found' or similar.
Thanks
Simon
-
Mar 7th, 2009, 01:57 PM
#2
Re: [2005] Error catching
Maybe something like this:
Code:
Try
'CHECK FOR AND CREATE DIRECTORY FIRST
Dim DirectoryName As String = Path.GetDirectoryName(pPath)
If Not Directory.Exists(DirectoryName) Then
Directory.CreateDirectory(DirectoryName)
End If
strLocalFile = New FileStream(pPath, FileMode.Create, FileAccess.Write, FileShare.None)
'
'
'
Catch ex As Exception
'HANDLE ERROR HERE
MessageBox.Show(ex.Message)
End Try
-
Mar 7th, 2009, 01:58 PM
#3
Re: [2005] Error catching
you can have Try...Chatch block to catch the excception thrown and sho a messegbox telling the user about the problem.
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
'...
'...
strLocalFile = New FileStream(pPath, FileMode.Create, FileAccess.Write, FileShare.None)
Catch ex As DirectoryNotFoundException
MessageBox.Show(ex.Message)
'you can chach some other exceptions as well.
Catch ex As FileNotFoundException
'...
End Try
End Sub
-
Mar 7th, 2009, 03:31 PM
#4
Thread Starter
Hyperactive Member
Re: [2005] Error catching
Thanks guys,
I'm getting an error at:
Code:
If Not Directory.Exists(DirectoryName) Then
Directory.CreateDirectory(DirectoryName)
End If
Saying that Name 'Directory' is not declared.
What am I missing?
Cheers
Simon
-
Mar 7th, 2009, 03:35 PM
#5
Re: [2005] Error catching
I think you are missing the IO namespace. Try
-
Mar 7th, 2009, 03:42 PM
#6
Thread Starter
Hyperactive Member
Re: [2005] Error catching
Thanks
Code:
If Not IO.Directory.Exists(DirectoryName) Then
IO.Directory.CreateDirectory(DirectoryName)
End If
Works great.
Simon
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|