M
Michael R. Pierotti
I have a app I have written that when the program is opened it logs out to a
logfile. What I can't figure out is that "X" button at the top. If a user
clicks on that instead of my menu item close how can I call the log sub and
write a closed program log entry ?
'Add any initialization after the InitializeComponent() call
CreateLFile()
Dim myear As String = Date.Now.Year.ToString
Dim mdate As String = Date.Now.Day.ToString
Dim mmonth As String = Date.Now.Month.ToString
Dim mtime As String = Date.Now.Hour.ToString
Dim mmin As String = Date.Now.Minute.ToString
Dim filename As String = "c:\log\" + myear + "-" + mmonth + "-" + mdate
+ " " + mtime + "-" + mmin + "_log.txt"
Public Function CreateLFile()
Dim fLen As Integer, filepath As String
filepath = filename
On Error Resume Next
fLen = Len(Dir$(filepath))
If fLen = 0 Then
Dim oFile As System.IO.File
Dim oWrite As System.IO.StreamWriter
oWrite = oFile.CreateText(filename)
oWrite.WriteLine("Opened new logfile " + filename + ".")
oWrite.Close()
Else
Dim w As StreamWriter = File.AppendText(filename)
Log("Reopened logfile.", w)
w.Close()
End If
End Function
Public Shared Sub Log(ByVal logMessage As String, ByVal w As TextWriter)
w.Write(ControlChars.CrLf & "Log Entry : ")
w.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(),
DateTime.Now.ToLongDateString())
w.WriteLine(" :")
w.WriteLine(" :{0}", logMessage)
w.WriteLine("-------------------------------")
' Update the underlying file.
w.Flush()
End Sub
Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MenuItem2.Click
Dim w As StreamWriter = File.AppendText(filename)
Log("Program Closed.", w)
w.Close()
Close()
End Sub
Thanks in advance for any help offered on this
Mike
logfile. What I can't figure out is that "X" button at the top. If a user
clicks on that instead of my menu item close how can I call the log sub and
write a closed program log entry ?
'Add any initialization after the InitializeComponent() call
CreateLFile()
Dim myear As String = Date.Now.Year.ToString
Dim mdate As String = Date.Now.Day.ToString
Dim mmonth As String = Date.Now.Month.ToString
Dim mtime As String = Date.Now.Hour.ToString
Dim mmin As String = Date.Now.Minute.ToString
Dim filename As String = "c:\log\" + myear + "-" + mmonth + "-" + mdate
+ " " + mtime + "-" + mmin + "_log.txt"
Public Function CreateLFile()
Dim fLen As Integer, filepath As String
filepath = filename
On Error Resume Next
fLen = Len(Dir$(filepath))
If fLen = 0 Then
Dim oFile As System.IO.File
Dim oWrite As System.IO.StreamWriter
oWrite = oFile.CreateText(filename)
oWrite.WriteLine("Opened new logfile " + filename + ".")
oWrite.Close()
Else
Dim w As StreamWriter = File.AppendText(filename)
Log("Reopened logfile.", w)
w.Close()
End If
End Function
Public Shared Sub Log(ByVal logMessage As String, ByVal w As TextWriter)
w.Write(ControlChars.CrLf & "Log Entry : ")
w.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(),
DateTime.Now.ToLongDateString())
w.WriteLine(" :")
w.WriteLine(" :{0}", logMessage)
w.WriteLine("-------------------------------")
' Update the underlying file.
w.Flush()
End Sub
Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MenuItem2.Click
Dim w As StreamWriter = File.AppendText(filename)
Log("Program Closed.", w)
w.Close()
Close()
End Sub
Thanks in advance for any help offered on this
Mike