Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
I am reading a Text file and checking the contents, in this instance all lines should be a maximum of 16 bytes in length. If there is invalid data then my form is redisplayed and the user can edit the text file before re-processing it. However, as the file has already been opened, my code doesn't work, it throws an exception that the file is in use ... and yes, I can see exactly why, but how do I handle / get around it ?!?

VB
Dim myTeamCount As Int16 = 0
Dim myTextFile As New System.IO.FileStream(OpenFileDialog1.FileName, IO.FileMode.Open)
Dim myReader As New System.IO.StreamReader(myTextFile)
Dim List As New List(Of String)
Do While myReader.Peek >= 0
    If myReader.ReadLine.Length > 16 Then
        MessageBox.Show("Record Longer Than 16 Characters in This File - " & myReader.ReadLine & " - Edit & Retry.", "ERROR")
        Me.Refresh()
        Exit Do
    End If
    myTeamCount = myTeamCount + 1
    List.Add(myReader.ReadLine)
Loop


Thanks in advance ...
Posted
Comments
[no name] 19-Jul-12 11:46am    
Any reason you could not read the entire file all at once?
Gary Heath 19-Jul-12 11:55am    
After I have checked every record, I then have to check the number of records, depending on a criteria that the user entered earlier, and if there is the correct number of records, then I want to write all of the stored records to a database Table.
[no name] 19-Jul-12 11:59am    
Yes I understand that. I would just use File.ReadAllText which gives you all of the file contents in an array of strings. Process the array and then when everything is correct, write the file back out from the array. Totally gets rid of this opening and closing files all the time and coordinating the process.
lewax00 19-Jul-12 12:04pm    
File.ReadAllLines might be more appropriate in this case.
Gary Heath 19-Jul-12 12:06pm    
OK, that's what AlluvialDeposit said, but he didn't explain ... I was looking to do this via an Array in the first place, but I kept reading that this " list (of t)" method was better !!! So many different ways of doing things in this language !!! I'll get googling, thanks ...

1 solution

You need to close the file before you can open it again, something like this should work:

VB
Dim myTeamCount As Int16 = 0
Dim myTextFile As New System.IO.FileStream(OpenFileDialog1.FileName, IO.FileMode.Open)
Dim myReader As New System.IO.StreamReader(myTextFile)
Dim List As New List(Of String)
Do While myReader.Peek >= 0
    If myReader.ReadLine.Length > 16 Then
        MessageBox.Show("Record Longer Than 16 Characters in This File - " & myReader.ReadLine & " - Edit & Retry.", "ERROR")
        myReader.Close()
        Me.Refresh()
        Exit Do
    End If
    myTeamCount = myTeamCount + 1
    List.Add(myReader.ReadLine)
Loop
 
Share this answer
 
Comments
Gary Heath 19-Jul-12 12:33pm    
This does indeed resolve the problem, but whilst running the logic through Debug, I added a MessageBox to display each record as it is processed, and the first record it displays is Record 2, then when it finds the record that is longer than 16, which is the 4th record, it displays the 5th record, so something is dodgy about my code !!! I'm off to research how to use the File.ReadAllText & File.ReadAllLines that have been suggested ... thanks to everybody for their help.
lewax00 19-Jul-12 12:52pm    
What you should be doing is storing the result of myReader.ReadLine in a string, because when you call it once to get the length, then again in the MessageBox call (which tells it to read the next line). I'd give you a code example but I haven't touched VB in years, so I don't remember exactly how right now. Or use System.IO.File.ReadAllLines to simplify it all ( http://msdn.microsoft.com/en-us/library/s2tte0y1.aspx )

Gary Heath 19-Jul-12 12:57pm    
Yes, just found how to use ReadAllLines ... so much easier !!! Thanks again ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900