Solved

Using streamwriter instead of printline in vb.net 2005

Posted on 2011-03-15
7
959 Views
Last Modified: 2013-11-07
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
0
Comment
Question by:RIAS
  • 4
  • 3
7 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 35139267
Try this out. You'll notice that I removed calls to close the file--"Using" should handle doing that for you.
Public Sub PrintToLog()
    'print  in the log file 
    Try
        If Not MessagesFile = String.Empty Then
            Using writer As New System.IO.StreamWriter(messagefile)
                writer.WriteLine(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
            End Using
        End If
    Catch exSystem As System.UnauthorizedAccessException
        Loading = False
        AddMessage(FormatMessage(20, MessagesFile))
        FlagStop = True
    Catch ex As System.IO.IOException
        Loading = False
        AddMessage(FormatMessage(20, MessagesFile))
        FlagStop = True
    Catch ex As Exception
        AddMessage(ex.ToString)
        AddMessage(FormatMessage(20, MessagesFile))
        FlagStop = True
    End Try
End Sub

Open in new window

0
 

Author Comment

by:RIAS
ID: 35139672
Cheers mate...will try and get back
0
 

Author Comment

by:RIAS
ID: 35139747
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
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35139824
My mistake. Modify line 5 to this:
Using writer As New System.IO.StreamWriter(messagefile, True)

Open in new window

0
 

Author Comment

by:RIAS
ID: 35139832
I think mate found the solution


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


Cheers for your help
0
 

Author Closing Comment

by:RIAS
ID: 35139841
Brilliant!!!!Works like charm!!!!!!!
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35139879
NP. Glad to help  : )
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question