Solved

VB .Net text output help needed

Posted on 2007-04-11
5
662 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

This article will show, step by step, how to integrate R code into a R Sweave document
This is an explanation of a simple data model to help parse a JSON feed
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 fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

760 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