Writing to a text file.

  • Thread starter Thread starter Iceman
  • Start date Start date
I

Iceman

I am logging things to a file but when I kill the app and
restart it, all the other logging is gone. How can I
prevent this form happening?
 
Iceman said:
I am logging things to a file but when I kill the app and
restart it, all the other logging is gone. How can I
prevent this form happening?

It sounds like your app is overwriting the file rather than appending to the
end of it.
 
Iceman said:
I am logging things to a file but when I kill the app and
restart it, all the other logging is gone. How can I
prevent this form happening?

Open the file in the Append mode.

If you use Microsoft.VisualBasic.FileSystem.FileOpen, pass OpenMode.Append
as the 3rd argument.

If you create a System.IO.FileStream object, use System.IO.FileMode.Append
as the file mode.

In addition, each time you write to the file, you should open the file,
write, and close it. If you don't close it after writing, some information
might be lost when the application is killed.
 
Here you go...

Private Sub logchange(ByVal source As Object, ByVal e As
System.IO.FileSystemEventArgs)

If e.ChangeType = IO.WatcherChangeTypes.Changed
Then

txt_folderactivity.Text &= vbCrLf & strUser
& " on " & strComputer & " - File " & e.FullPath & _
" has been modified - " & Now()

SaveTextToFile
(txt_folderactivity.Text, "c:\Log.txt")

End If

If e.ChangeType = IO.WatcherChangeTypes.Created
Then

txt_folderactivity.Text &= vbCrLf & strUser
& " on " & strComputer & " - File " & e.FullPath & _
" has been created - " & Now()

SaveTextToFile
(txt_folderactivity.Text, "c:\Log.txt")

End If

If e.ChangeType = IO.WatcherChangeTypes.Deleted
Then

txt_folderactivity.Text &= vbCrLf & strUser
& " on " & strComputer & " - File " & e.FullPath & _
" has been deleted - " & Now()

SaveTextToFile
(txt_folderactivity.Text, "c:\Log.txt")

End If

End Sub

Public Sub logrename(ByVal source As Object, ByVal e
As System.IO.RenamedEventArgs)

txt_folderactivity.Text &= vbCrLf & strUser & "
on " & strComputer & " - File " & e.OldName & _
" has been renamed to " & e.Name & vbCrLf

SaveTextToFile
(txt_folderactivity.Text, "c:\Log.txt")

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top