Working with text files + 2005

Posted on 2007-10-08
Last Modified: 2010-04-23
I need to write out a text file, I used to use Print #lFileNum, text, text, text in VB6

What is the best approach now?
Question by:rocky050371
    LVL 14

    Expert Comment

    you can us a stream writer object

            Dim st As StreamWriter = New StreamWriter("c:\dd.txt", True)
            st.Write("you value")
    LVL 10

    Expert Comment

    The FileSystemObject [Microsoft Scripting Runtime] is what I always use - good support for anything other than binary files.
    LVL 48

    Expert Comment

    If you like to follow the Microsoft example (snippets) using the new My Class ...

    ' Read
    Dim fileinfo As String
    fileinfo = My.Computer.FileSystem.ReadAllText("c:\yourfile.txt")

    ' Write
    My.Computer.FileSystem.WriteAllText("c:\yourfile.txt", "text", True)


    Author Comment

    I need to write it out line by line. The document may have several hundred lines, is this still the best way of achieving it
    LVL 48

    Accepted Solution

    The write parameter in the following sentence it's to append in your file. You can always add line by line
    My.Computer.FileSystem.WriteAllText("c:\yourfile.txt", "text", True)

    To read line by line you can do
    Dm fileinfo() As String
    fileinfo = My.Computer.FileSystem.ReadAllText("c:\yourfile.txt")
    For x As Integer = 0 To fileinfo.Lenght-1
    Next x

    LVL 62

    Assisted Solution

    by:Fernando Soto
    Hi rocky050371;

    Imports System.IO

    ' Open the file up for writing
    Dim sw As StreamWriter = New StreamWriter("c:\MyDirectory/MyFile.txt")

    To write to the file:

    Dim LineOfData As String = "Data to be written to the file"
    ' Write to the file with no line terminator
    sw.Write( LineOfData )

    ' Or write to the file with line terminator
    sw.WriteLine( LineOfData )

    ' When you are done with the file close it.
    sw.Close( )

    LVL 96

    Assisted Solution

    by:Bob Learned
    System.IO.File.WriteAllLines(fileName, contents)

    LVL 1

    Expert Comment

    Forced accept.

    EE Admin

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Article by: Kraeven
    Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    737 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

    22 Experts available now in Live!

    Get 1:1 Help Now