Solved

Using streamwriter instead of printline in vb.net 2005

Posted on 2011-03-15
7
949 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 74

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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 74

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 74

Expert Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now