2015年10月27日 星期二

VB.NET 建立自己的錯誤紀錄檔



Imports System.IO
Public Class TEST1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim plog As String = Application.StartupPath & "\sqlexe_error\"
        Dim fileName As String = plog & "err" & DateTime.Now.ToString("yyyy-MM-dd_HHmmssff") & ".txt"

        '判斷是否有記錄檔資料夾
        If Directory.Exists(plog) = False Then
            Directory.CreateDirectory(plog)
        End If

        Try
            '紀錄瀏覽選擇的路徑
            Dim sw As StreamWriter = New StreamWriter(fileName)
            sw.WriteLine("錯誤測試")
            sw.Write("建檔日期:")
            sw.WriteLine(DateTime.Now)
            sw.Close()
            sw.Dispose()
        Catch ex As Exception
            MsgBox("發生錯誤")
        End Try

    End Sub

End Class

沒有留言:

張貼留言