Solved

VB .Net text output help needed

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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Simple Linear Regression
Six Sigma Control Plans

717 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