Link to home
Start Free TrialLog in
Avatar of cofneverlivetotell
cofneverlivetotell

asked on

VB .Net text output help needed

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
Avatar of cofneverlivetotell
cofneverlivetotell

ASKER

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
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()

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()
ASKER CERTIFIED SOLUTION
Avatar of VBRocks
VBRocks
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks, VBRocks - The StringBuilder solution worked best, though it formats better for notepad to use Chr(13)'s instead of vbCrLf.

Points awarded, Cheers.