Public Sub ShowError(strModule As String, strProcedure As String, lngErrorNumber As Long, strErrorDescription As String, showMsg As String)
網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、小程序制作、集團企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了民權(quán)免費建站歡迎大家使用!
'
'錯誤處理中心過程,寫數(shù)據(jù)庫日志表或?qū)懭罩疚募?/p>
'
'strModule '模塊名稱
'strProcedure '過程名稱
'lngErrorNumber '錯誤ID號
'strErrorDescription '錯誤描述
'showMsg '是否顯示本過程內(nèi)錯誤顯示信息(值:"Y" or "N")
'Error表結(jié)構(gòu)(f001 (Date)發(fā)生時間, f002 (nvarchar50)模塊名稱, f003 (nvarchar50)過程名稱, f004 (nvarchar50)錯誤ID號, _
f005 (nvarchar300)錯誤描述,f006 (nvarchar50)版 本 號, f007 (nvarchar50)用戶名稱, f008 (nvarchar50)網(wǎng)卡地址
'ErrorCode表結(jié)構(gòu) f001 (nvarchar20)錯誤代碼, f002 (nvarchar255)錯誤信息, f003 (numeric9)錯誤級別
' 級別說明: '10'以下,一般錯誤,不影響操作
' '11-20',嚴重錯誤,不能操作,程序執(zhí)行退出
On Error GoTo ErrorHandle
Dim strMessage As String
Dim strCaption As String
Dim sVer As String
Dim intLogFile As Integer
Dim Res As New ADODB.Recordset
Dim ResErrorCode As New ADODB.Recordset
Dim strSQL As String
'對應(yīng)錯誤號,從ErrorCode表中找到對應(yīng)的錯誤信息,0-1000 錯誤號保留給VB
DBOpen ResErrorCode, "select * from errorcode where f001='" lngErrorNumber "'"
If Not (ResErrorCode.EOF Or ResErrorCode.BOF) Then
strMessage = ResErrorCode.Fields("f002")
If ResErrorCode.Fields("f003") 10 Then
MsgBox "產(chǎn)生一個嚴重錯誤,可能影響到系統(tǒng)的可操作性,請立即聯(lián)系本系統(tǒng)開發(fā)人員!", vbCritical, "嚴重錯誤"
End If
End If
'寫錯誤入文件----------------------------
intLogFile = FreeFile
Open App.Path "\" strIni.LogFile For Append As #intLogFile
Print #intLogFile, "***錯誤"; VBA.Now "*** " "Version:" _
str$(App.Major) "." str$(App.Minor) "." Format(App.Revision, "0000")
Print #intLogFile, "Error: " lngErrorNumber
Print #intLogFile, "Description: " strErrorDescription
Print #intLogFile, "Module: " strModule
Print #intLogFile, "Procedure: " strProcedure
Print #intLogFile, ""
Close #intLogFile
If Len(strMessage) 2 Then strErrorDescription = strMessage
strMessage = "錯誤: " "(" lngErrorNumber ")" strErrorDescription vbCrLf vbCrLf _
"模塊:" strModule "; 過程:" strProcedure
sVer = Trim(str$(App.Major) "." str$(App.Minor) "." _
Format(App.Revision, "0000"))
strCaption = "錯誤 Version: " sVer
'寫錯誤入數(shù)據(jù)庫表--------------------------
strSQL = "insert into error(f001,f002,f003,f004,f005,f006,f007,f008) values(" _
DateFmtB VBA.Now DateFmtE "," _
IIf(Len(Trim(strModule)) = 0, "null", "'" strModule "'") "," _
IIf(Len(Trim(strProcedure)) = 0, "null", "'" strProcedure "'") "," _
IIf(Len(Trim(lngErrorNumber)) = 0, "null", "'" lngErrorNumber "'") "," _
IIf(Len(Trim(strErrorDescription)) = 0, "null", "'" Replace(strErrorDescription, "'", "") "'") "," _
IIf(Len(Trim(sVer)) = 0, "null", "'" sVer "'") "," _
IIf(Len(Trim(sUserName)) = 0, "null", "'" sUserName "'") "," _
IIf(Len(Trim(sVer)) = 0, "null", "'" EthernetNO "'") ")"
Cn.Execute strSQL
'是否顯示未知錯誤信息
If Trim(UCase(showMsg)) = "Y" Then MsgBox strMessage, vbCritical, strCaption
PROC_EXIT:
Set Res = Nothing
Set ResErrorCode = Nothing
Exit Sub
ErrorHandle:
Resume Next
在程序目錄下生成log目錄,用于保存日志文件。
自動在log目錄中,生成日志文件,文件命名用年月日定義,如20100119_log.log。
在日志文件中,每行記錄一個時間點的運行內(nèi)容,時間點的精度應(yīng)該達到毫秒。
軟糖來告訴你吧。
VB.net中讀寫文件主要使用System.IO命名空間。
① 使用 File.ReadAllText 讀取
Dim s As String = System.IO.File.ReadAllText("C:\a.txt")
② 使用 StreamReader 讀取,注意編碼格式和寫入的編碼保持一致。
Dim sr As StreamReader = New StreamReader("C:\a.txt", System.Text.Encoding.UTF8)
Dim s As String = sr.ReadToEnd()
sr.Close()
③ 使用 File.WriteAllText 寫入,會覆蓋同名的文件。
Dim 要寫的內(nèi)容 As String = ""
File.WriteAllText(文件路徑, 要寫的內(nèi)容, System.Text.Encoding.UTF8)
④ 使用 StreamWriter 寫入。
Dim sw As System.IO.StreamWriter = New System.IO.StreamWriter("C:\a.txt", False, System.Text.Encoding.UTF8)
sw.WriteLine(TextTB.Text)
sw.Close()
⑤ 使用 StreamWriter 追加寫入。
將上面代碼的第二個參數(shù)False改為True。
◆ 滿意請采納,謝謝 ◆
寫入:Dim sr As New IO.StreamWriter(Application.StartupPath "/寫入的文本.txt")
sr.WriteLine("寫入的內(nèi)容") sr.Close()讀?。篒f (File.Exists(Application.StartupPath "/msg.txt")) Then
Dim fm As New IO.FileStream(Application.StartupPath "/讀取的文本.txt", FileMode.Open)
Dim sr As IO.StreamReader = New IO.StreamReader(fm)
Do While sr.Peek() = 0
TextBox1.Text = sr.ReadLine() (讀取文本到文本框)
Loop end if