Text Files

  • 14 years ago

    I am trying to fix up the code below which I tried to follow from the MSDN Library.

    The problem I face is this: The code below DOES create the text file Test2.txt, it does print hello and goodbye but it prints it like this:

    h e l l o g o o d b y e  <--------- spaced out and all on same line

    What I want to do is print it like this:

    hello

    <carriage return>

    goodbye

    I have tried inserting Chr(10) , Chr(13) but all that does is insert a box character into the text file. I have also tried vbCrLf and that did nothing either.

    What do I need to fix to get the text to display properly?

    Module1 contains the declaration:

    Imports

    System.IO

    Imports

    System.Text

    Form1 contains the code:

    Dim objStreamWriter As IO.StreamWriter

    'Open the file.

    objStreamWriter =

    New IO.StreamWriter("C:\Test2.txt", True, System.Text.UnicodeEncoding.Unicode)

    'Write out text

    objStreamWriter.Write("hello")

    objStreamWriter.Write("goodbye")

    'Close the file.

    objStreamWriter.Close()

    End Sub

  • 14 years ago

    If you want a line break between the two words then call WriteLine("hello") instead of Write("hello").  Alternatively you could call Write(Environment.NewLine) in between but why add an extra line of code when the function to do what you want already exists?

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“It works on my machine.” - Anonymous