File opertation in VB.Net

Friday, December 26, 2008

imports system.io
Public Class Form1
    Public Sub MaintainintoFile(Optional ByVal StringtoWrite As String = "Error Not Trapped")
        Try
            Dim StartLog As Boolean
            Dim FName As String

            Dim LogDirName As String
            Dim BaseErr As String = ""
            Dim fs As FileStream
            Dim fileWrite As StreamWriter
            Dim dir As String
            StartLog = True
            dir = Application.StartupPath
            LogDirName = dir & "\Log\"
            If Not Directory.Exists(LogDirName) Then Directory.CreateDirectory(LogDirName)

            FName = LogDirName & "Log.log"
            If File.Exists(FName) Then
                fs = New FileStream(FName, FileMode.Append, FileAccess.Write, FileShare.Read)
                fileWrite = New StreamWriter(fs)
            Else
                fs = New FileStream(FName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read)
                fileWrite = New StreamWriter(fs)
            End If
            fileWrite.Write(vbNewLine & CStr(Now) & ":" & StringtoWrite)
            fileWrite.Close()
            Exit Sub
        Catch ex As Exception
            MsgBox(Err.Description)
        End Try
    End Sub
End Class

0 comments

Post a Comment