Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

VB .Net text output help needed

Posted on 2007-04-11
5
Medium Priority
?
668 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
[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
  • 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 1000 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

Technology Partners: 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!

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…
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Loops Section Overview

610 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