Link to home
Start Free TrialLog in
Avatar of RIAS
RIASFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Using streamwriter instead of printline in vb.net 2005

Hi,
Any suggestions on how to convert the following in streamwriter instead of printline.

Cheers

Public Sub PrintToLog()
      'print  in the log file
      Try
         If Not MessagesFile = String.Empty Then
            MsgFileUnit = FreeFile() 'Get next free file
            If MsgFileOpen OrElse frmmain.rbtbatch.Checked Then
               FileOpen(MsgFileUnit, MessagesFile, OpenMode.Append) 'Open the message file for append. Don't lose existing data
            ElseIf (Not ClearFile AndAlso Not frmmain.rbtbatch.Checked) Then
               FileOpen(MsgFileUnit, MessagesFile, OpenMode.Output) 'Open the message file for output. It clears all existing data
               MsgFileOpen = True
               ClearFile = True
            End If
            PrintLine(MsgFileUnit, List.Item(List.Count - 1).SubItems(0).Text.ToString.PadRight(10) & " " & List.Item(List.Count - 1).SubItems(1).Text) 'Output same message to messages text file                                               'Get next message
            FileClose(MsgFileUnit) 'Close the message file so the user can access it at any time
         End If
      Catch exSystem As System.UnauthorizedAccessException
         Loading = False
         FileClose(MsgFileUnit)
         AddMessage(FormatMessage(20, MessagesFile))
         FlagStop = True
      Catch ex As System.IO.IOException
         Loading = False
         FileClose(MsgFileUnit)
         AddMessage(FormatMessage(20, MessagesFile))
         FlagStop = True
      Catch ex As Exception
         AddMessage(ex.ToString)
         AddMessage(FormatMessage(20, MessagesFile))
         FileClose(MsgFileUnit)
         FlagStop = True
      End Try
   End Sub
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RIAS

ASKER

Cheers mate...will try and get back
Avatar of RIAS

ASKER

Hi,
The problem here is it opens and closes the file everytime it i called thus wiping off the data written in it previously.My old code was appending the file.

    FileOpen(MsgFileUnit, MessagesFile, OpenMode.Append)


Any suggestions?

Cheers
My mistake. Modify line 5 to this:
Using writer As New System.IO.StreamWriter(messagefile, True)

Open in new window

Avatar of RIAS

ASKER

I think mate found the solution


 Using writer As New System.IO.StreamWriter(messagefile,append)


Cheers for your help
Avatar of RIAS

ASKER

Brilliant!!!!Works like charm!!!!!!!
NP. Glad to help  : )