Jump to content


Writing to Text Files


  • Please log in to reply
5 replies to this topic

#1 Dylan

Dylan

    Advanced Member

  • Root Admin
  • PipPipPip
  • 1,060 posts
  • Location:Atlanta, GA, USA
  • Interests:Computer Science, Operating Systems

  • VB Knowledge: Experienced
  • VB Version:VB 2008 (.NET 3.0/3.5)
  • OS: Mac OS X

Posted 24 June 2007 - 10:50 PM

Instead of using System.IOs file streams, stream writers, etc., Visual Basic 2005 allows us to modify files using the My namespace. I like to use the My namespace for text writing because its easy! Lets get started.


If you just want to create your text file perform this:

My.Computer.FileSystem.WriteAllText(FilePath, "", False)

We are actually writing "" (blank) to the text file. By default, the file will be created if it doesn't exist.

If you want to write text to the newly made file just do this:

My.Computer.FileSystem.WriteAllText(FilePath, "text to be written", False)

If you want to append an existing file, just change False to true.

If you want to delete the file we made just run this:

My.Computer.FileSystem.DeleteFile(FilePath)

I hope that this tutorial will help you better under stand the FileSystem class in the My namespace and show you how the FileSystem class can easily replace the System.IOs file streams and stream writers/readers.

#2 Nirinium

Nirinium

    Member

  • Members
  • PipPip
  • 25 posts

  • VB Knowledge: Decent
  • VB Version:VB 2005 (.NET 2.0)

Posted 22 July 2007 - 04:46 PM

NICE.

#3 Dan Verssen

Dan Verssen

    Advanced Member

  • Members
  • PipPipPip
  • 36 posts
  • Location:California

  • VB Knowledge: Decent
  • VB Version:VB 2005 (.NET 2.0)

Posted 11 August 2007 - 10:16 PM

Based on your code, I'm using the following to read in the contents of a text file after making sure it exists.

Quote

If File.Exists(filepath) Then
            Label3.Text = My.Computer.FileSystem.ReadAllText(filepath)
        End If

How do I read in one line at a time from the file so I can store them in subscripted variables? Each of the lines in the file have a vbNewLine added on.

Example when I append the file...

My.Computer.FileSystem.WriteAllText(filepath, Form1.heroname & vbNewLine, True)


#4 Dylan

Dylan

    Advanced Member

  • Root Admin
  • PipPipPip
  • 1,060 posts
  • Location:Atlanta, GA, USA
  • Interests:Computer Science, Operating Systems

  • VB Knowledge: Experienced
  • VB Version:VB 2008 (.NET 3.0/3.5)
  • OS: Mac OS X

Posted 11 August 2007 - 10:36 PM

Just read the files contents into a string. Then, split the string by vbNewLine.

Dim fileText as string = My.Computer.FileSystem.ReadAllText("Path")
Dim arr() as String = fileText.Split(vbNewLine)

You then can manipulate the array to do what you would like.

#5 Zenzija

Zenzija

    Advanced Member

  • Members
  • PipPipPip
  • 230 posts
  • Gender:Male
  • Location:Washington, USA
  • Interests:Gaming, Programming, Graphics Designing, and Web development!

  • VB Knowledge: Beginner
  • VB Version:Other
  • OS: Windows 7 x64

Posted 09 January 2008 - 06:22 PM

View PostDylan, on Aug 11 2007, 07:36 PM, said:

Just read the files contents into a string. Then, split the string by vbNewLine.

Dim fileText as string = My.Computer.FileSystem.ReadAllText("Path")
Dim arr() as String = fileText.Split(vbNewLine)

You then can manipulate the array to do what you would like.
Would this work with a non-text file, say a Configuration File? How much difference would there be in the coding?

#6 Robin

Robin

    Newbie

  • Members
  • Pip
  • 1 posts

  • VB Knowledge: Beginner
  • VB Version:VB 2005 (.NET 2.0)

Posted 11 August 2008 - 05:01 AM

question deleted i already found the awnser ;)
thnx for tutorial




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users