Results 1 to 6 of 6

Thread: [RESOLVED] Open and create log file

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2009
    Posts
    29

    Resolved [RESOLVED] Open and create log file

    Hi,

    How can I open and create file in VB.NET as I do in VB6?

    Dim lotno As String = "123456"
    FileOpen(1, (Application.StartupPath & "\Log\" & lotno & ".txt"), OpenMode.Append)
    PrintLine(1, "Start Time :", Now())
    FileClose(1)

  2. #2
    Addicted Member
    Join Date
    Jun 2009
    Posts
    245

    Re: Open and create log file

    You can do this:

    vb.net Code:
    1. Dim fs As New FileStream("C:\somwhere\on\your\disk.log", FileMode.Create, FileAccess.Write)
    2.         Dim s As New StreamWriter(fs, System.Text.Encoding.Default)
    3.         s.BaseStream.Seek(0, SeekOrigin.End)
    4.         s.WriteLine("Something")
    5.         s.Close()

    Just remember to import the System.IO namespace

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2009
    Posts
    29

    Re: Open and create log file

    hi erik,

    thanks for ur reply..

    but how can I use OpenMode.Append in vb.net coz I need to create a file in existing file witout deleting the old data in the file

  4. #4
    Addicted Member
    Join Date
    Oct 2009
    Posts
    212

    Re: Open and create log file

    maybe this will help
    Code:
    Public Sub WriteData(ByVal strData As String, ByVal dir As String, ByVal path As String, ByVal append As Boolean)
            'Check directory first
            If Not (System.IO.Directory.Exists(dir)) Then
                System.IO.Directory.CreateDirectory(dir)
            End If
    
            Dim objReader As System.IO.StreamWriter
            Try
                objReader = New System.IO.StreamWriter(path, append)
                objReader.Write("Time: " & Now() & " LOG: " & strData & vbCrLf)
                'objReader.WriteLine(vbCrLf) 'uncomment for blank line between entries
                objReader.Close()
            Catch Ex As Exception
                'Throw New Exception(Ex.Message)
            End Try
        End Sub

  5. #5
    Member
    Join Date
    Nov 2006
    Posts
    54

    Re: Open and create log file

    and this might also help... at least you have options
    i actually have a log module in my application and im currently using this code.
    I didn't import anything because i already specified the package in the declarations

    Code:
    Dim myFile As System.IO.File, _
         fWriter As System.IO.StreamWriter, _
         fName, fLog As String
    
    'string to be written in the text file
    fLog = Now.ToString("hh:mm tt") & vbTab & "LOGINFO" & vbTab & "userAction"
    'file path and name
    fName = "C:\Logs\ERROR_LOG_" & Now.ToString("ddMMyyyy") & ".txt"
    
    'if file is existing already: the boolean parameter is set to true to append a new line on the file
    If myFile.Exists(fName) Then
        fWriter = New System.IO.StreamWriter(fName, True)
    Else
    'if it does not exist, create a new text
        fWriter = myFile.CreateText(repFName)
    End If
    
    fWriter.WriteLine(fLog)
    fWriter.Close()
    fWriter = Nothing
    Quote Originally Posted by shxrainz View Post
    hi erik,

    thanks for ur reply..

    but how can I use OpenMode.Append in vb.net coz I need to create a file in existing file witout deleting the old data in the file
    Last edited by fantasy2ville; Oct 22nd, 2009 at 09:24 PM. Reason: typo errors

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Sep 2009
    Posts
    29

    Re: Open and create log file

    hi all,

    Thanks for the info.

    I just find the solution by using FileMode.Append which it will append the text into the existing file and it work.

    'Append to file : FileMode.Append
    Dim fs As New FileStream((Application.StartupPath & "\Log\" & lotno & ".txt"), FileMode.Append, FileAccess.Write)
    Dim s As New StreamWriter(fs, System.Text.Encoding.Default)
    s.BaseStream.Seek(0, SeekOrigin.End)
    s.WriteLine(countrows & " " & Now() & " " & mfgnum & " " & "PASS")
    s.WriteLine(countrows & " " & Now() & " " & mfgnum & " " & "PASS")
    s.Close()

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width