Working with txt Files

Talk2Tom11

Well-known member
Joined
Feb 22, 2004
Messages
144
Location
Westchester, NY
I am running into a little problem. I am having my program look for a txt file based on info that is put in from the user. The problem is I cant find a way to give an error msgbox if the file is not found. My code is below.

Code:
Dim sr As IO.StreamReader = IO.File.OpenText(txtBoxLast.Text + txtBoxFirst.Text + ".TXT")
        Dim Found As Boolean = False


Do While (sr.Peek() <> -1) And (Not Found)
            Last = sr.ReadLine
            First = sr.ReadLine
            Gender = sr.ReadLine
            Age = sr.ReadLine
            room = sr.ReadLine
            id = sr.ReadLine
            extension = sr.ReadLine
            guests = sr.ReadToEnd


            If txtBoxLast.Text = Last Then
                txtBoxGender.Text = Gender
                txtBoxAge.Text = Age
                txtBoxRoom.Text = room
                txtBoxID.Text = id
                txtBoxextension.Text = extension
                txtBoxGuest.Text = guests
                Found = True
            End If
        Loop
        sr.Close()
        If Not Found Then
            message = ("Name Not Found.  Be sure to check the spelling")
            MsgBox(message, , "Information Not Found")
        End If
 
you cud always enclose the file processing between a try catch block and catch a filenotfound exception
// c# code

try
{
StreamReader sr = new StreamReader(txtBoxLast.Text + txtBoxFirst.Text);
//file processing done here
}
catch (FileNotFoundException fex)
{
MessageBox.Show(fex.Message);
}
 
Back
Top