Hello, I'm writing a Visual Basic.net (vb.net 03) program and I'm
checking if file(s) exist in a particular directory. If the program
finds such files, I will write the total and names in my log and keep
going. Otherwise, If I find none, I need to write that in my log and
exit the program.
My problem lies when there's no files in that the directory. I can
only get the total (0), but that doesn't get caught as an exception. I
tried different exceptions, but I guess the fact that there are no
files doesn't cause an exception. This is what I have:
Imports System.IO
Imports System.Text
Dim sw As New StreamWriter("c:\mylog.log", True)
Try
' Only get files that have extension *.txt
Dim dirs As String() =
Directory.GetFiles("c:\MyTextFilesDir\", "*.txt")
sw.WriteLine("The number of files with an *.txt extension
is {0}.", dirs.Length)
Dim dir As String
For Each dir In dirs
sw.WriteLine(dir)
Next
Catch ex As FileNotFoundException ' <= I need to modify this!
sw.WriteLine("The process failed: {0}", ex.ToString())
sw.WriteLine("")
sw.WriteLine("Unable to get text file name.")
Exit Sub
End Try
sw.Flush()
sw.Close()
Does anyone knows how to catch when there's no files in a dir and make
that as an exception? Or.. do I need to catch exceptions, or just
check if the total exisinting files = 0?
Thanks
JR.