|
-
Apr 9th, 2006, 07:39 AM
#1
Thread Starter
Lively Member
[RESOLVED] clearing a text file
I have this code that reads a logfile and stores the lastline as a string in "lastline"
It then checks to see if the log file is >= 10 lines long and if it is opens a srteamwrither so it clears the logfile. I do this because the log file can get very long and i figured the program will run faster if it only has to read 10 lines at the most form the file rather then 400. the problem im having is it gives me an error saying theat the file is in use by another process. I dont get this error if i remove the code for the streamwriter that clears the file, why is this?
-------------------------------------------------------------------------
Public Shared Sub logreader(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs)
If e.ChangeType = IO.WatcherChangeTypes.Changed Then
Dim p As Integer
p = 0
currentline = ""
Dim fs As New System.IO.FileStream(CurrentEq2LogFile, IO.FileMode.OpenOrCreate, IO.FileAccess.ReadWrite, FileShare.ReadWrite)
Dim sr As New System.IO.StreamReader(fs)
Do While p <> -1
lastline = ""
'reads the log file line by line until the end of the log reached
If sr.Peek <> -1 Then
currentline = sr.ReadLine 'holds the last line read from the log file
line = line + 1
End If
If sr.Peek = -1 Then
p = -1
lastline = currentline
End If
Loop
fs.Close()
sr.Close()
Call praselog()' this sub checks to see if last line contains a certant word
End If
Call clearlog()
End Sub
Public Shared Sub clearlog()
If line >= 10 Then
Dim clearfile As New System.IO.StreamWriter(CurrentLogFile)
line = 0
clearfile.Close()
End If
End Sub
-
Apr 9th, 2006, 08:39 AM
#2
Re: clearing a text file
What is in your praselog sub? Chances are you are leaving a streamreader or streamwriter open on that file there or some other place, so it cant open a new one on that file because you haven't called close on the previous one.
-
Apr 9th, 2006, 08:48 AM
#3
Thread Starter
Lively Member
Re: clearing a text file
the praselog sub just has a few if then statements there arent any streams.
-
Apr 9th, 2006, 08:53 AM
#4
Re: clearing a text file
You can probably simpify this code by doing something like...
VB Code:
'loops until end of file
While sr.Peek <> -1
'code here
End While
***Removed post below since I didnt see your end loop line. You can put your code in [ vbcode ] [ /vbcode ] tags so it can be more readable
Last edited by gigemboy; Apr 9th, 2006 at 09:06 AM.
-
Apr 9th, 2006, 09:27 AM
#5
Thread Starter
Lively Member
Re: clearing a text file
Ok thanx gigemboy I did what you sugested and you were right thanx!
Not only does it work now but my code is much less confusing too lol.
here is the new code.
-----------------------------------------------------------------------
VB Code:
Public Shared Sub logreader(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs)
If e.ChangeType = IO.WatcherChangeTypes.Changed Then ' the following code will only execute if file is changed
Dim fs As New System.IO.FileStream(CurrentLogFile, IO.FileMode.OpenOrCreate, IO.FileAccess.ReadWrite, FileShare.ReadWrite)
Dim sr As New System.IO.StreamReader(fs)
Do While sr.Peek <> -1
lastline = sr.ReadLine' lastline is a string that holds the last line of the log file
line = line + 1' counts how many lines were read
Loop
fs.Close()
sr.Close()
fs.Dispose()
sr.Dispose()
End If
Call Cleanlog()
Call checkiflastlinecontains()
End Sub
Public Shared Sub cleanlog()
If line >= 10 Then
filewatcher.EnableRaisingEvents = False 'stops the logreader sub event monitor
Dim sw As New System.IO.StreamWriter(CurrentLogFile, False)
'writes a blank log file
line = 0
sw.Close()
sw.Dispose()
filewatcher.EnableRaisingEvents = True ' restarts the logreader event monitor
Else : line = 0
End If
End Sub
Last edited by chaos75; Apr 9th, 2006 at 09:40 AM.
-
Apr 9th, 2006, 09:51 AM
#6
Re: [RESOLVED] clearing a text file
I think you can refrain from calling .Dispose after calling .Close on the Streamreaders and StreamWriters, because I believe the .Close method disposes of the object as well (I have never had to explicitly call .Dispose on it).
-
Jan 17th, 2007, 05:11 PM
#7
Junior Member
Re: [RESOLVED] clearing a text file
I'm running into kind of the same thing.
Same situation. If there are changes I streamread the text file and read the contents into a textbox and trigger different actions based on what is is read.
i do the
read.close()
My problem is that the thread does not close in time to open the reader again the next time the routine fires. If i pause 20 seconds after the reader.close() it works fine. Otherwise the file is still locked.
I want it to read as fast as the file changes....is there any other way than with streamreader to do this?
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
|