Results 1 to 3 of 3

Thread: [RESOLVED] Trouble writing to a .txt file

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Location
    Madison, WI
    Posts
    136

    Resolved [RESOLVED] Trouble writing to a .txt file

    I am trying to write to a .txt file here is my code:

    VB Code:
    1. Dim oFile As System.IO.File
    2.         Dim oWrite As System.IO.StreamWriter
    3.         oWrite = oFile.CreateText("C:\New.txt")
    4.         oWrite.WriteLine("This is just a test.")
    5.         oWrite.WriteLine()

    The code will create a new txt file, but it will not write the text to the file.

    thanks

  2. #2
    Fanatic Member MetalKid's Avatar
    Join Date
    Aug 2005
    Location
    Green Bay, Wisconsin
    Posts
    534

    Re: Trouble writing to a .txt file

    Add these 2 lines at the end:

    oWrite.Flush()
    oWrite.Close()

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Trouble writing to a .txt file

    First off, CreateText is a Shared method so you should just qualify it with the class name rather than a variable name, i.e.
    VB Code:
    1. oWrite = IO.File.CreateText("C:\New.txt")
    Secondly, you have call Close on the StreamWriter to flush the data to the file and close it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width