|
-
Apr 23rd, 2007, 12:10 PM
#1
Thread Starter
Hyperactive Member
Exit Sub if file in array doesn't exist
I have been trying to figure out how to exit this sub if the number of files in di.GetFiles is 0 but I have not been able to do so. Can someone please help me with this?
Code:
Dim di As New IO.DirectoryInfo(strFPath)
Dim diar1 As IO.FileInfo() = di.GetFiles()
Dim dra As IO.FileInfo
Dim message As New MailMessage
message.From = New MailAddress(StrMailFrom)
message.To.Add(strMailTo1)
message.To.Add(strMailTo2)
message.To.Add(strMailTo3)
message.Subject = StrSubject
message.Body = StrBody
For Each dra In diar1
message.Attachments.Add(New Attachment(dra.FullName))
Next
Dim emailClient As New SmtpClient(strMailServer)
emailClient.Credentials = (SMTP_Cred)
emailClient.DeliveryMethod = SmtpDeliveryMethod.Network
emailClient.EnableSsl = True
Try
emailClient.Send(message)
Catch ex As Exception
MessageBox.Show(ex.Message & vbNewLine & ex.StackTrace)
End Try
-
Apr 23rd, 2007, 12:12 PM
#2
Re: Exit Sub if file in array doesn't exist
directly under the GetFiles line:
VB Code:
If diar1.Length = 0 Then
Exit Sub
End If
-
Apr 23rd, 2007, 12:28 PM
#3
Re: Exit Sub if file in array doesn't exist
Multiple return points from a method are generally a sign of spaghetti logic. As a rule of thumb you should aim to have only a single return point: at the end of the function.
For example, instead of using:
Code:
If diar1.Length = 0 Then
Exit Sub
End If
You should use:
Code:
If (diar1.Length > 0) Then
' rest of code here...
End If
-
Apr 23rd, 2007, 03:16 PM
#4
Thread Starter
Hyperactive Member
Re: Exit Sub if file in array doesn't exist
That seems to have worked, although I won't be a 100% sure until I am able to test it in the production environment tomorrow. I do have one other semi-related question though.
At some point in these three Dim statements
Dim di As New IO.DirectoryInfo(strFPath)
Dim diar1 As IO.FileInfo() = di.GetFiles()
Dim dra As IO.FileInfo
I am holding the entire path and file name in memory. So now I am trying to use that full path in this Move from the Try Catch Block so that I can move the files to another directory once the email has been sent.
Code:
Try
emailClient.Send(message)
' Put file and path name here to move file.
My.Computer.FileSystem.MoveFile(something here, "K:\Databases\OrderComplete")
Catch ex As Exception
MessageBox.Show(ex.Message & vbNewLine & ex.StackTrace)
Finally
message.Dispose()
End Try
What I am doing wrong?
Last edited by FastEddie; Apr 23rd, 2007 at 03:47 PM.
-
Apr 23rd, 2007, 03:53 PM
#5
Re: Exit Sub if file in array doesn't exist
 Originally Posted by FastEddie
I am holding the entire path and file name in memory. So now I am trying to use that full path in this Move from the Try Catch Block so that I can move the files to another directory once the email has been sent.
Code:
Try
emailClient.Send(message)
' Put file and path name here to move file.
My.Computer.FileSystem.MoveFile(something here, "K:\Databases\OrderComplete")
Catch ex As Exception
MessageBox.Show(ex.Message & vbNewLine & ex.StackTrace)
Finally
message.Dispose()
End Try
What I am doing wrong?
I don't have VS 2005, but I know in 2003 you need to define the full path (including the file name) for your destination.
So you would change this...
VB Code:
My.Computer.FileSystem.MoveFile(something here, "K:\Databases\OrderComplete")
To this...
VB Code:
My.Computer.FileSystem.MoveFile(something here, "K:\Databases\OrderComplete\SomeFileName.txt")
-
Apr 23rd, 2007, 04:40 PM
#6
Thread Starter
Hyperactive Member
Re: Exit Sub if file in array doesn't exist
I think the Destination Path is OK. It is the source path where I have the 'something here' text that I need help with.
I would have thought there would be something like a dra.FullName that I could put in the Source File so that the code like this.
Code:
My.Computer.FileSystem.MoveFile(dra.FileName, "K:\Databases\OrderComplete")
I suspect that if I do end up with a problem on the destination side of the Move File I should be able to use the answer to the first question to make it work.
-
Apr 23rd, 2007, 10:10 PM
#7
Thread Starter
Hyperactive Member
Re: Exit Sub if file in array doesn't exist
I got it.
Code:
MoveAllFiles(dra.FullName, "K:\Databases\CompletedOrders\" & dra.Name)
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
|