在软件中常用到日志文件,这是我在项目中用到的一个操作日志文件的类。
Imports System
Imports System.IO
Public Class ClassLogFile
Inherits System.Object
Public Shared logFileName As String
Public Sub WriteLog(ByVal LogString As String)
Dim path As String = "Log.txt"
Dim sw As StreamWriter
' This text is added only once to the file.
If File.Exists(path) = False Then
' Create a file to write to.
sw = File.CreateText(path)
sw.WriteLine("本日志文件记录了您的日常操作的重要信息!对您的应用有重要的帮助作用!")
sw.Flush()
sw.Close()
End If
sw = File.AppendText(path)
sw.WriteLine(Format(Now(), "yyyy-MM-dd HH:mm:ss") & Space(4) & LogString)
'清理当前编写器的所有缓冲区,并使所有缓冲数据写入基础流。
sw.Flush()
sw.Close()
End Sub
Public Sub ReadLogWithNotePad() 'ByVal PATH As String)
' Open the file to read from.
Try
WriteLog("查阅Log")
System.Diagnostics.Process.Start("Log.txt")
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Public Sub ClearLog()
System.IO.File.Delete("log.txt")
WriteLog(" 清空Log")
End Sub
End Class
' ====================
' 使用实例:
'实例化 日志文件
Module1.gLogFile = New ClassLogFile
'记录日志
gLogFile.WriteLog("系统启动")
‘===============
如果觉得有用,有空就留言。