?
Solved

Using streamwriter instead of printline in vb.net 2005

Posted on 2011-03-15
7
Medium Priority
?
981 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

649 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