?
Solved

Using streamwriter instead of printline in vb.net 2005

Posted on 2011-03-15
7
Medium Priority
?
987 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 2000 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
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month17 days, 6 hours left to enroll

864 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