Solved

VB .Net text output help needed

Posted on 2007-04-11
5
663 Views
Last Modified: 2013-11-26
Visual Basic 2005 Express Edition, Windows 2000 Pro.

I'm having some problems with a file output, I need to generate a file of HTML from a loop, with non-loop HTML before and after.
I'm using this code to create the file with looped HTMl and after, but the problem comes with getting some before the loop output.

~~~~~~~~~~~~~~~~~~~~

        Dim strOutputFilePath As String
        Dim strImagePath As String
        Dim intPosition As Integer
        Dim intEnd As Integer

        strOutputFilePath = "C:\Output.html"
        strImagePath = "C:\img\"
        intEnd = lstQuestionList.Items.Count - 1
        intPosition = 0

        Dim OutputArray(intEnd) As String

        While intPosition <= intEnd
            OutputArray(intPosition) = "<p><img src=""" + strImagePath + lstList.Items.Item(intPosition) + ".jpg""></p>"
            intPosition = intPosition + 1
        End While

        System.IO.File.WriteAllLines(strOutputFilePath, OutputArray)
        System.IO.File.AppendAllText(strOutputFilePath, "End of page")

~~~~~~~~~~~~~~~~~


The WriteAllLines function needed to output the array causes anything in the file to be overwritten, and so I can't add any text before this.

Any ideas, experts?

Thanks :D
0
Comment
Question by:cofneverlivetotell
  • 3
  • 2
5 Comments
 
LVL 2

Author Comment

by:cofneverlivetotell
ID: 18888865
Oh, and incase you need clarification of the output that code gives, here's an example.

<p><img src="C:\img\1.jpg"></p>
<p><img src="C:\img\2.jpg"></p>
<p><img src="C:\img\3.jpg"></p>
<p><img src="C:\img\4.jpg"></p>
<p><img src="C:\img\5.jpg"></p>
<p><img src="C:\img\6.jpg"></p>
End of page
0
 
LVL 27

Expert Comment

by:VBRocks
ID: 18889966
Perhaps a StreamWriter will work for you:

        Dim sw As New System.IO.StreamWriter(strOutputFilePath)

        While intPosition <= intEnd
            sw.WriteLine("<p><img src=""" + strImagePath + lstList.Items.Item(intPosition) + ".jpg""></p>")

            intPosition = intPosition + 1
            sw.WriteLine()
        End While

        sw.Close()
        sw.Dispose()

0
 
LVL 27

Expert Comment

by:VBRocks
ID: 18889974
Perhaps a StreamWriter will work for you:

        Dim sw As New System.IO.StreamWriter(strOutputFilePath)

        While intPosition <= intEnd
            sw.WriteLine("<p><img src=""" + strImagePath + lstList.Items.Item(intPosition) + ".jpg""></p>")

            intPosition = intPosition + 1
            'sw.WriteLine()    <My mistake, remove this line.
        End While

        sw.Close()
        sw.Dispose()
0
 
LVL 27

Accepted Solution

by:
VBRocks earned 250 total points
ID: 18890033
Another approach could be to use a string builder:

        Dim strOutputFilePath As String
        Dim strImagePath As String
        Dim intPosition As Integer
        Dim intEnd As Integer

        Dim sb As New System.Text.StringBuilder()

        strOutputFilePath = "C:\Output.html"
        strImagePath = "C:\img\"
        intEnd = lstQuestionList.Items.Count - 1
        intPosition = 0

        Dim OutputArray(intEnd) As String

        sb.Append("<html><head></head><body>")

        While intPosition <= intEnd
            sb.Append("<p><img src=""" + strImagePath + lstList.Items.Item(intPosition) + ".jpg""></p>" & vbCrLf)

            intPosition = intPosition + 1
        End While

        sb.Append("</body></html>")

        System.IO.File.WriteAllLines(strOutputFilePath, sb.ToString().Split(vbCrLf))
        System.IO.File.AppendAllText(strOutputFilePath, "End of page")

0
 
LVL 2

Author Comment

by:cofneverlivetotell
ID: 18896727
Thanks, VBRocks - The StringBuilder solution worked best, though it formats better for notepad to use Chr(13)'s instead of vbCrLf.

Points awarded, Cheers.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

815 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

8 Experts available now in Live!

Get 1:1 Help Now