CanOz Blog

Neil Knobbe's Blog at vbCity

This blog hosted by:
http://blogs.vbcity.com      
  Home :: Syndication  :: Login

OctNovember 2005Dec
SMTWTFS
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

Archives

Image Galleries

vbCity Blogs

Monday, November 28, 2005 #

In the previous post I talked about Reading Files.  Now I will show you how to Write Files.

 

Writing to Files is as easy as Reading from Files.  As with Reading Files you need to Import the System.IO Namespace.

 

Imports System.IO

 

As with the StreamReader, writing with StreamWriter can be done in more than one way.  How you Write to the File will also depend on if you want to Write to a New File, Overwrite and existing File or Appending and existing File.

 

Let’s take a look at Writing to a New File.

 

Dim sw As New StreamWriter(PathToFileHere)  ' Declare your StreamWriter and File Path

Dim MyString As String  ' Declare the String you will write to the File

 

MyString = “Blah, Blah, Blah”  ' Set the Value of your String

 

sw.Write(MyString)  ' Write the String to the File

 

sw.Close()  ' Close the StreamWriter

 

The above code will create the File, if it does not Exist and write the String to the File.  (*Caution is advised here as the same code will Overwrite the contents of an existing File.)

 

If you want to Append an Existing File there are a couple of ways to go about it.  As always when working with files you need to add the Imports Statement.

 

Imports System.IO

 

Then you need to add the code that will Append the File.

 

Dim sw As StreamWriter = File.AppendText(PathToFileHere)

Dim MyString As String

 

MyString = “Blah, Blah, Blah”

 

sw.Write(Environment.NewLine & MyString)

 

sw.Close()

 

You can also use WriteLine()

 

Imports Sytsem.IO

 

Dim sw As New StreamWriter(PathToFileHere)

Dim MyString As String

 

MyString = “Blah, Blah, Blah”

 

sw.WriteLine(Environment.NewLine & MyString)

 

sw.Close()

 

Hopefully this quick look at Writing to Files gives you a start on Writing Files.

posted @ 7:48 PM

Depending on your Project you could find yourself needing to read Files.  One way that you can accomplish this is to use the StreamReader.  StreamReader is part of the System.IO Namespace and you must add the Imports Statement to your Project before you can access the StreamReader.  At the very top of your Form.vb code page, before anything else, you would type:

 

Imports System.IO

 

Now your Project can access the System.IO Namespace.  (Admittedly you could access the System.IO Namespace without using the Imports Statement.  You would just need to put System.IO in front of all the Classes of the IO Namespace that you use in your Project.  For example System.IO.StreamReader or System.IO.File.OpenText, but I prefer using the Imports Statement.)

 

The next thing you want to do is to declare your StreamReader.  A couple different ways of doing this are:

 

Dim sr As New StreamReader(PathToFileHere)

 

' or

 

Dim sr As StreamReader

sr = File.OpenText(PathToFileHere)

 

Depending on what you want to do with the contents of the File, you can Read the contents of the File Line by Line or the entire Length of the File.

 

Read entire Length of a File

 

Imports System.IO  ' Add Imports Statement to top of Form Code

 

Dim sr As New StreamReader(PathToFileHere)  ' Declare the StreamReader and Open the File

 

Dim MyString As String  ' Declare a String Variable to hold the contents of the File.

 

MyString = sr.ReadToEnd()  ' Read the contents of the file into the String Varaible

 

txtTextBox1.Text = MyString  ' Display the contents of the File.

 

Read a File Line by Line

 

Imports System.IO  ' Add Imports Statement to top of Form Code

 

Dim sr As StreamReader  ' Declare the StreamReader

 

sr = File.OpenText(PathToFileHere)  ' Open the File

 

Do While sr.Peek <> -1  ' While Peek can read Characters from the File (Peek Returns the next Character until there are no more then will Return a -1).

    sr.ReadLine() ' Read the next Line of the File

    txtTextBox1.Text &= sr.ReadLine & Environment.NewLine ' Display the Line of the File

Loop

 

sr.Close()

 

As with just about everything in programming there are several different ways to accomplish a task.  These are just two ways that you can go about reading the contents of Files.
posted @ 7:43 PM